Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81501 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 68492 invoked from network); 31 Jan 2015 22:11:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Jan 2015 22:11:04 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.170 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.170 mail-we0-f170.google.com Received: from [74.125.82.170] ([74.125.82.170:37757] helo=mail-we0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A9/00-01632-7F25DC45 for ; Sat, 31 Jan 2015 17:11:03 -0500 Received: by mail-we0-f170.google.com with SMTP id w55so27417214wes.1 for ; Sat, 31 Jan 2015 14:10:59 -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=antfIi6qPl1GU8I8ZSNUBjqf2peE+SQa8w61lnDzjgo=; b=G97XuSWQYMWt82OrRBdYHFlP7GdoIqwsAvIrv9Hek4r6PGzddqfwUBRefeNDi+M5ed oVKXlGf2PoXvspm/VvcMOI+btYgUiC0ifyTkZ6y1FswJkryaHQROZmtoqIPKgL9GdZux donAZ9eS4YXTzIlPnNvE2tkegjAuz2WNW+Y3/m+sTQhq9MTfUPbgrSTektW3tK9ROagR 8HSQjQJ2n4ntyBvzh/17VivDQjd5mwn4tMYVuCDGCVkDeQZLxthc3a9YLFVgC2nnKz1l AZ9CAU6x6UuAmPN8ecXp6oU8btvyt5djqpNISBVyAgeODnUgV2YC9WKqHk+3oFH5iPOI DRdw== X-Received: by 10.195.13.104 with SMTP id ex8mr28032479wjd.12.1422742257876; Sat, 31 Jan 2015 14:10:57 -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 cb14sm12823004wib.22.2015.01.31.14.10.56 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 31 Jan 2015 14:10:56 -0800 (PST) Message-ID: <54CD52D9.4070105@gmail.com> Date: Sat, 31 Jan 2015 22:10:33 +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@lists.php.net References: 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 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. > > > Perhaps we should use the keyword "this" instead of "caller"? :) > Then the code in PHP will look like this: > > > 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: 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]