On 31/01/2015 19:41, S.A.N wrote:
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.
Hi,
I think I get what you're trying to do, but I'm not sure the PHP code
you suggest is quite equivalent to the JavaScript. In JavaScript, you
are setting a single function (getHolder) as a direct member of your
objects:
oA.getHolder = getHolder
oB.getHolder = getHolder
But in the PHP, you are attaching a whole object to a property:
$a->object = $c;
$b->object = $c;
When you call getHolder in PHP, the "holder" of that method is always
$c, because you could split the code up like this:
$something = $a->object;
$something->getHolder();
In general, a method can't be copied from one object to another in PHP
like it can in JS, so the concept of dynamic this doesn't normally make
any sense. However, you can do something similar by "binding a closure";
you just need to invoke it a bit differently because properties and
methods are not interchangeable like they are in JS:
<?php
class A {
public $getHolder;
}
class B {
public $getHolder;
}
$getHolderDefinition = function() { return get_class($this); };
$a = new A;
$b = new B;
$a->getHolder = $getHolderDefinition->bindTo($a);
$b->getHolder = $getHolderDefinition->bindTo($b);
echo $a->getHolder->__invoke(), "\n";
echo $b->getHolder->__invoke(), "\n";
// Or
$method = $a->getHolder;
echo $method(), "\n";
$method = $b->getHolder;
echo $method(), "\n";
For many callback / event scenarios, you don't actually need to treat
the closure as a property / method of the object at all, you just want
the $this reference, so the ->bindTo() part is all you need, maybe more
like this:
class SomeEventTarget {
function triggerEvent($someCallback) {
$boundCallback = $someCallback->bindTo($this);
$boundCallback($some, $arguments);
}
}
(PS: The house rules for this list asks for replies to be below quoted
text, not above.)
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php