2015-02-01 21:41 GMT+02:00 Rowan Collins <rowan.coll...@gmail.com>:
> 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

Yes you are right.
In PHP, i added to $holder a sub-object, for two reasons:

1. In PHP, it is impossible
    $holder->call = function(){};
    $holder->call(); // Error

2. I need to have a common state in $this.
   That's why i decided to add a subobject: $holder->object->call();

But i understand that this is a bad idea.
i originally thought that it simplifies the implementation of
solutions in PHP, but it is not.

Maybe correct option, native implement in PHP - pattern prototype,
then do not need to create new keywords and questionable behavior.

Let me show you an example JS, i want to do in PHP:
<script language="JavaScript">

var A = {}
var B = {}
var C =
{
    object:
    {
        index: 0
    },
    method: function ()
    {
        console.log(this.object.index++)
    }
}

A.__proto__ = C
B.__proto__ = C

A.method() // return 0
B.method() // return 1
C.method() // return 2

</script>

In PHP, I would like to have an analog method Object.create
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.5
Perhaps the option to add in PHP, new magic static method:
ClassName::__create($object).
This method will create a new instance of the class and introduce API
from specified object.

It would also be very nice to have a method of changing the prototype
at run time, this could be used method: $a->__proto($other)
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-additional-properties-of-the-object.prototype-object

Then the PHP code looks like this:
<?php

class A{}
class B{}
class C
{
    public $object;

    public function __construct()
    {
        $this->object = new stdClass;
        $this->object->index = 0;
    }

    public function method()
    {
        return $this->object->index++;
    }
}

$c = new C;
$a = A::__create($c);
$b = B::__create($c);

$a->method() // return 0
$b->method() // return 1
$c->method() // return 2

?>

If it is technically possible and PHP developers is interesting, then
it is better to create a new topic, to discuss implementation of the
prototype in PHP.

I do not want to break the paradigm of PHP and not impose everywhere
and always use prototypes, I hope that PHP will have a multi-paradigm.

ECMAScript not had classes, but version 6+ implemented class, is nice.
If PHP 7 will be implemented prototypes will be very cool to everyone.

Thank you all.

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

Reply via email to