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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to