# reference caller object

- Thread: https://externals.io/message/45580
- Original archive: https://news-web.php.net/php.internals/45580
- Started by Chris Trahey on 2009-09-16T20:59:16+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #45580 — Chris Trahey — 2009-09-16T20:59:16+00:00

URL: https://externals.io/message/45580#45580

(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]

---

## #45581 — Larry Garfield — 2009-09-17T01:47:45+00:00

URL: https://externals.io/message/45580#45581 · In reply to: #45580

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

---

## #45582 — Chris Trahey — 2009-09-17T03:01:13+00:00

URL: https://externals.io/message/45580#45582

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:
> > […]
> who is
> > […]
> call.
> > […]
> "parent" (actualy
> > […]
> dev_execption();
> > […]
> admin_exception('module
> > […]
>
> --
> Larry Garfield
> larry@garfieldtech.com

---

## #45583 — Richard Quadling — 2009-09-17T09:09:52+00:00

URL: https://externals.io/message/45580#45583 · In reply to: #45582

2009/9/17 Chris Trahey <ctrahey@csisd.org>:
> 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
>
> 
>
> > […]
>

Isn't this exactly what the backtrace is for?

-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

---

## #45593 — Chris Stockton — 2009-09-17T16:46:04+00:00

URL: https://externals.io/message/45580#45593 · In reply to: #45580

> (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:
> .. snip ..

Type hinting would be a clean approach, I can not see a argument why
the below would be less correct.

public function registerMe(Loadable $oLoadable) {
       if( ! ($this->authenticateMod($oLoadable))) throw new
admin_exception('module
not active');
       $this->loadedMods[$oLoadable->module_id()] = $oLoadable;
       return $oLoadable->onLoad();
}
