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

From: Date: Fri, 30 Jan 2015 04:30:26 +0000
Subject: Re: Use "caller" keyword, to access object caller.
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
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

?>

2015-01-30 5:50 GMT+02:00 Juan Basso <[email protected]>:

> Usually it is solved sending the $this instance to class C constructor and
> C object storing it in some attribute, solving the problem. I don't think a
> new keyword would help.
>
> Juan Basso
>
> On Thu, Jan 29, 2015 at 9:53 PM, S.A.N <[email protected]> wrote:
>
>> The reason for creating circular references, usually due to the need to
>> bind objects.
>>
>> But this relationship can often be obtained from the context of the call.
>>
>> It will be very convenient to have a keyword that will return reference to
>> an object, which caused this object.
>>
>> Sorry for my English, I'm not a native speaker.
>> A simple example below shows the behavior that we need.
>>
>> <?php
>>
>> class A
>> {
>>     public function __construct() {
>>         $this->object = new C;
>>     }
>> }
>>
>> class B
>> {
>>     public function __construct() {
>>         $this->object = new C;
>>     }
>> }
>>
>> class C
>> {
>>     public function getCaller() {
>>         return caller::class;
>>     }
>> }
>>
>> $a = new A;
>> $b = new B;
>> $c = new C;
>>
>> $a->object->getCaller(); // return A
>> $b->object->getCaller(); // return B
>>
>> $c->getCaller(); // Fatal Error - undefined caller context
>>
>> ?>
>>
>> Create a new keyword can cause problems with backward compatibility...
>> Perhaps you can solve a problem to using an existing keyword?
>>
>> Thank you for any feedback.
>>
>
>


Thread (19 messages)

« previous php.internals (#81399) next »