On 31/01/2015 23:42, S.A.N wrote:
Yes, you're right, in PHP you can solve this problem by other methods, I know...

But the problem is that PHP is no nice and convenient for solving this problem.
So I suggested to add new keyword, not to do manually bindTo($this)
for each methods.

Have a closer look at my example; the point is that without bindTo, there is no way of doing the *first* part of your JS example, i.e. attaching the same function/method to two objects - there is no equivalent of this line:

oA.getHolder = getHolder


In your PHP example, you introduce an extra object, $c, which doesn't exist in your JS example; if it did, it would look like this:

var oA = new A
var oB = new B
var oC = new C

oA.myC = oC;
oB.myC = oC;
oC.getHolder = getHolder

oA.myC.getHolder() // returns C
oB.myC.getHolder() // returns C


Hopefully you can see that that makes all the difference - myHolder is now always called from the same object, the one created with "new C".

The reason this doesn't translate well is that In JS, a method is just a property which happens to be a closure, so "this" is necessarily dynamic - there is no class definition to define the relationship between the method and the object (ES6 introduces all sorts of variations and caveats to this, all of which can be emulated in older versions). In PHP, a method is fundamentally different from functions, closures, and properties, despite similarities in syntax, so none of the same behaviours would make sense.

Regards,

--
Rowan Collins
[IMSoP]


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

Reply via email to