Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81502 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 72763 invoked from network); 31 Jan 2015 23:42:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Jan 2015 23:42:16 -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.178 as permitted sender) X-PHP-List-Original-Sender: ua.san.alex@gmail.com X-Host-Fingerprint: 209.85.213.178 mail-ig0-f178.google.com Received: from [209.85.213.178] ([209.85.213.178:54784] helo=mail-ig0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 55/70-01632-5586DC45 for ; Sat, 31 Jan 2015 18:42:14 -0500 Received: by mail-ig0-f178.google.com with SMTP id hl2so10127299igb.5 for ; Sat, 31 Jan 2015 15:42:11 -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=lIwrCVnybWgVlg89Hv+hpI79u5TZc1bFkJoVxyMex7k=; b=RM6U2CzLCBgbTnyiCaopjya16/mH4nLFJhunCoixrPWOFdb/pWiaC/jR9uQFgP+kh8 vJB4ZHn8r0k/I0XXUfgs7UeUkUYJWv1+6fRAEOCQNdFvE9pinLpqg+vcz7Pwh3dMhGnK ugqX9CspGlhEMV2UHPqivHKCMszTlE1ZMX3j98bsXtURE2eSo7juuY+Je1r5mAXEcXAo dN9YMSDLauLOjx0k2skg9OaZTfYO9PP561yvlQa2+JoJ2mqFjRESzOlICgerjNA/ynEo BP2wkXcTQ7WmYQB13uuj5Ck/9W0ZgWllTY9TR7UUdxY7pZG9CJm6X0mYq8z5joixhFKJ s8Jw== MIME-Version: 1.0 X-Received: by 10.50.142.106 with SMTP id rv10mr4470291igb.18.1422747731148; Sat, 31 Jan 2015 15:42:11 -0800 (PST) Received: by 10.50.93.3 with HTTP; Sat, 31 Jan 2015 15:42:11 -0800 (PST) In-Reply-To: <54CD52D9.4070105@gmail.com> References: <54CD52D9.4070105@gmail.com> Date: Sun, 1 Feb 2015 01:42: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 0:10 GMT+02:00 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: > > > class A { > public $getHolder; > } > class B { > public $getHolder; > } > > $getHolderDefinition = function() { return get_class($this); }; > > $a = new A; > $b = new B; > > $a->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] > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > 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. Thank.