Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81545 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 43059 invoked from network); 2 Feb 2015 01:58:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Feb 2015 01:58:17 -0000 Authentication-Results: pb1.pair.com header.from=ua.san.alex@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ua.san.alex@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.179 as permitted sender) X-PHP-List-Original-Sender: ua.san.alex@gmail.com X-Host-Fingerprint: 209.85.213.179 mail-ig0-f179.google.com Received: from [209.85.213.179] ([209.85.213.179:43598] helo=mail-ig0-f179.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C8/00-41900-7B9DEC45 for ; Sun, 01 Feb 2015 20:58:15 -0500 Received: by mail-ig0-f179.google.com with SMTP id l13so13962904iga.0 for ; Sun, 01 Feb 2015 17:58:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=MEuejPdTbbs+it+gDrBiyBoMbf7RahOUjtIV/YEjWqE=; b=MFdaI3OA8OTKzdjkqJRBTB/JWkQRkY2tJ7VC8dqBpGaMLZ+Bll4Tm0IS8HsjKDRFQa C1L0im/Uby6O/IzQlaYjV60bFjAkSYkQnoFOnlQdmg5/FakJQsrgCl54b4S0EWsuL9vL kUPxIsiW4giiji5Aw+yOTXMYi2yCqQldEbRmTXrnWhOfJ9ZQMK5UpnYVPaYc06NOyvDX Lr3XW2IFhZhe64hs9wX+ufcYZ2XLKRVHKbaMX6LozXEiRM6D+TaVvrgdZJssfSn44/h/ c82zU9l1Q7ezqOMFXQGpfnB5V3v/tyEA0uiemXW5Cl5L4v1r2Or9YGpg8P05STUt91Rb 9fSg== MIME-Version: 1.0 X-Received: by 10.107.169.35 with SMTP id s35mr14347711ioe.50.1422842292083; Sun, 01 Feb 2015 17:58:12 -0800 (PST) Received: by 10.50.93.3 with HTTP; Sun, 1 Feb 2015 17:58:11 -0800 (PST) In-Reply-To: <54CE817A.1020703@gmail.com> References: <54CD52D9.4070105@gmail.com> <54CE817A.1020703@gmail.com> Date: Mon, 2 Feb 2015 03:58:11 +0200 Message-ID: To: Rowan Collins Cc: internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Use "caller" keyword, to access object caller. From: ua.san.alex@gmail.com ("S.A.N") 2015-02-01 21:41 GMT+02:00 Rowan Collins : > 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: 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: 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.