No, I'm not describing this behavior, but I chose the wrong name
keyword, this is not perceived correctly.

My source code examples, it is clear that no returns previous object
from the call stack, me need return references to the holder object.

It's more like the dynamic of "this" in JavaScript.
Dynamic "this", sometimes very convenient for the event model, so I
proposed to implement this feature in PHP.
In PHP, there traits, extends, but it's all static bind, in runtime
can not be added or redefined.

I'll show a simple JS example, in which needed behavior dynamic this.
<script language="JavaScript">

function A(){}
function B(){}

function getHolder()
{
    console.log(this.constructor.name)
}

var oA = new A
var oB = new B

oA.getHolder = getHolder
oB.getHolder = getHolder

oA.getHolder() // return A
oB.getHolder() // return B

getHolder() // return Window

</script>

Perhaps we should use the keyword "this" instead of "caller"? :)
Then the code in PHP will look like this:

<?php

class A
{
    public $object;
}

class B
{
    public $object;
}

class C
{
    public function getHolder() {
        return this::class;
    }
}

$a = new A;
$b = new B;
$c = new C;

$a->object = $c;
$b->object = $c;

$a->object->getHolder(); // return A
$b->object->getHolder(); // return B

$c->getHolder(); // Fatal Error - undefined context

?>

What do you think?
Thank.

2015-01-30 18:43 GMT+02:00 Ferenc Kovacs <tyr...@gmail.com>:
>
>
> On Fri, Jan 30, 2015 at 3:53 AM, S.A.N <ua.san.a...@gmail.com> 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.
>
>
> see the previous discussion on this topic:
> http://grokbase.com/t/php/php-internals/099g86x7k6/reference-caller-object
> I think having it as a magic constant would be better, but I still don't
> like the idea (it is already possible, there aren't really a good usecase
> for it, the example you mentioned is a prime example how would it be misused
> to cause debugging nightmares, etc.).
>
> --
> Ferenc Kovács
> @Tyr43l - http://tyrael.hu

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

Reply via email to