Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:45581 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 10501 invoked from network); 17 Sep 2009 01:47:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Sep 2009 01:47:51 -0000 Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 69.5.8.4 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 69.5.8.4 hc02.futurequest.net Linux 2.4/2.6 Received: from [69.5.8.4] ([69.5.8.4:40798] helo=hc02.futurequest.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D2/D1-29403-64591BA4 for ; Wed, 16 Sep 2009 21:47:50 -0400 Received: (qmail 1592 invoked from network); 17 Sep 2009 01:47:47 -0000 X-Originating-IP: [24.13.255.226] To: internals@lists.php.net Date: Wed, 16 Sep 2009 20:47:45 -0500 User-Agent: KMail/1.11.2 (Linux/2.6.27-14-generic; KDE/4.2.2; i686; ; ) References: In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-ID: <200909162047.45365.larry@garfieldtech.com> Subject: Re: [PHP-DEV] reference caller object From: larry@garfieldtech.com (Larry Garfield) If the object needs to behave differently based on who called it, there's already a very easy way to do that: class A { function foo() { $b = new B(); $b->bar($this); } } class B { function bar($caller) { if ($caller instance of A) { // ... } } } That's also therefore much more self-documenting and easier to test (because you can simulate what gets passed in) than a magic keyword. I don't see a need for new magic constants here. On Wednesday 16 September 2009 3:59:16 pm Chris Trahey wrote: > (Please direct me elsewhere if necessary, this is a feature request) > > It would be handy to allow a method to behave differently based on who is > calling it. > the function I am looking for would essentially do this: > > function getCaller(){ > $bt = debug_backtrace(); > return $bt[2]['object']; > } > But of course, there is a lot of uneccessary processing with that call. > Perhaps it could be implimented similar to "self" and "parent" (actualy > more like "static") keywords. > > So you could do: > public function registerMe() { > if( ! (caller instanceof 'my_interface')) throw new dev_execption(); > if( ! ($this->authenticateMod(caller))) throw new admin_exception('module > not active'); > $this->loadedMods[caller->module_id()] = caller; > return caller->onLoad(); > } > > Chris Trahey > Web Applications Developer > Database Administrator > CSISD [Technology] -- Larry Garfield larry@garfieldtech.com