Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81541 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22766 invoked from network); 1 Feb 2015 19:42:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Feb 2015 19:42:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.176 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.176 mail-wi0-f176.google.com Received: from [209.85.212.176] ([209.85.212.176:37107] helo=mail-wi0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 35/F3-33325-7918EC45 for ; Sun, 01 Feb 2015 14:42:16 -0500 Received: by mail-wi0-f176.google.com with SMTP id bs8so12671287wib.3 for ; Sun, 01 Feb 2015 11:42:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=IW2uIQ0hBJU0/wQeQIpa+khvxmsU+aenHFviv12jL2c=; b=SA2BKCD3GMkaBkZIJ1/yKwF4SwoE2j4I7Dm2FWyeuN+EVlCmVYpfmB9f7uNj48UtKV DRCM1tdO+Jrzt5m0tGtY6H3yshAAlTQe54qIjmcVeZ9+b+zdGMPMOniVsmevT8OBE8wG PMMqCo+w5IJsN6k0lE16qlak+lr6CUIlnzjv+zSm41kFpdVQbmEUy7I1065q63YKIUud MG8f1iOwsB86HsQ7p7f1CYPJoZeMw3dOCrbtjPXtEaSV839eQqCb73qKETVgvgO8AUqw bD/iHKrpdAnS9jIu0kqdQCc00FYufDAjb6VC8YR/6iibRySztwLerqhZecqu5xXVCN/d xt5A== X-Received: by 10.180.81.98 with SMTP id z2mr17474326wix.40.1422819732167; Sun, 01 Feb 2015 11:42:12 -0800 (PST) Received: from [192.168.0.2] (cpc68956-brig15-2-0-cust215.3-3.cable.virginm.net. [82.6.24.216]) by mx.google.com with ESMTPSA id ku8sm24544579wjb.23.2015.02.01.11.42.11 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 01 Feb 2015 11:42:11 -0800 (PST) Message-ID: <54CE817A.1020703@gmail.com> Date: Sun, 01 Feb 2015 19:41:46 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: internals References: <54CD52D9.4070105@gmail.com> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Use "caller" keyword, to access object caller. From: rowan.collins@gmail.com (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 [IMSoP]