Re: reference caller object

From: Date: Thu, 17 Sep 2009 03:01:13 +0000
Subject: Re: reference caller object
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-45582@lists.php.net to get a copy of this message
It seems worthwhile in a service-provider situation where you do not wish to enforce use of $this in service consumers. The service provider can impliment caller without modifying existing code. Semantically, it should be considered redundant to pass $this in a method call. There should be an implicit way for a message recipient to determine who is sending the message, in a uniform way (not left up to the consumers of your API to impliment). Thanks for your consideration. Chris Trahey Sent from my iPhone On Sep 16, 2009, at 8:47 PM, Larry Garfield <larry@garfieldtech.com> wrote:
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 --


« previous php.internals (#45582) next »