Re: Use "caller" keyword, to access object caller.

From: Date: Fri, 30 Jan 2015 04:43:00 +0000
Subject: Re: Use "caller" keyword, to access object caller.
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On 01/29/2015 10:30 PM, S.A.N wrote:
I will clarify the benefits of the keyword. Single instance of the class can reused in different contexts calls. <?php class A {
     public $object;
} class B {
     public $object;
} class C {
     public function getCaller() {
         return caller::class;
     }
} $a = new A; $b = new B; $c = new C; $a->object = $c; $b->object = $c; $a->object->getCaller(); // return A $b->object->getCaller(); // return B $c->getCaller(); // Fatal Error - undefined caller context ?>
This seems like an extremely bad idea to do in the first place. Code in class C should function the same whether it was called from $a or $b. If it should vary, that should be made explicit in the object's construction and you have DIFFERENT instances of it. Having an object that behaves differently depending on an implicit relationship with where it happened to be called from is full of all kinds of impossible to debug magic. I don't even know how you'd unit test it. I can think of no use case where this wouldn't cause only problems. --Larry Garfield

Thread (19 messages)

« previous php.internals (#81400) next »