Re: reference caller object
| From: | Larry Garfield | Date: | Thu, 17 Sep 2009 01:47:45 +0000 |
| Subject: | Re: reference caller object | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-45581@lists.php.net to get a copy of this message | ||
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