Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:47431 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 92668 invoked from network); 19 Mar 2010 16:07:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Mar 2010 16:07:43 -0000 Authentication-Results: pb1.pair.com header.from=mathieu.suen@easyflirt.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=mathieu.suen@easyflirt.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain easyflirt.com designates 91.199.255.56 as permitted sender) X-PHP-List-Original-Sender: mathieu.suen@easyflirt.com X-Host-Fingerprint: 91.199.255.56 python-06.easyrencontre.com Linux 2.6 Received: from [91.199.255.56] ([91.199.255.56:41521] helo=mail.easyflirt.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 24/32-14064-B41A3AB4 for ; Fri, 19 Mar 2010 11:07:41 -0500 Received: from [192.168.0.51] (office.easyrencontre.com [78.155.152.6]) by mail.easyflirt.com (Postfix) with ESMTPSA id 051C56374E2; Fri, 19 Mar 2010 17:07:35 +0100 (CET) Message-ID: <4BA3A147.2020506@easyflirt.com> Date: Fri, 19 Mar 2010 17:07:35 +0100 User-Agent: Thunderbird 2.0.0.24 (X11/20100317) MIME-Version: 1.0 To: "Ionut G. Stan" CC: internals@lists.php.net References: <4BA0DF61.1010907@easyflirt.com> <4BA0E39C.7020600@gmail.com> <4BA0E9E8.8000404@easyflirt.com> <4BA1DB19.1080608@easyflirt.com> <660eb66f1003180224g5662ba1dtb123c3ee88a3a3e5@mail.gmail.com> <4BA25919.8090805@easyflirt.com> <4BA37E11.9080403@gmail.com> In-Reply-To: <4BA37E11.9080403@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Assign array with __get From: mathieu.suen@easyflirt.com ("mathieu.suen") Right I could work around the issue with the return by reference without any problem. I am still thinking that if you try to write a meta-circular interpreter you gonna work very hard to make this subtleties worked. And according to "Shriram Krishnamurthi" in his textbook PLAI: " a truly powerful language is one that makes it easy to write its meta-circular interpreter." -- Mathieu Suen Ionut G. Stan wrote: > I guess what Mathieu is trying to say is that this: > > class Foo > { > public $bar = array(); > } > > $foo = new Foo; > $foo->bar[3] = 1; > > var_dump($foo); > > > > > ...is inconsistent with this: > > class Foo > { > public function __get($property) > { > if (! isset($this->$property)) { > $this->$property = array(); > } > > return $this->$property; > } > } > > $foo = new Foo; > $foo->bar[3] = 1; > > var_dump($foo); > > > > > ...or even this: > > class Foo > { > private $bar = array(); > > public function __get($property) > { > return $this->$property; > } > } > > $foo = new Foo; > $foo->bar[3] = 1; > > var_dump($foo); > > > Now, I'm not really sure this is that bad, as there may be use cases > where one would like to return different values every time __get is > called. And, as I wrote in a previous email, there are ways to work > around this inconsistency by using return by reference, so everybody > can be happy. I think. > > > > On 3/19/10 4:56 AM, Etienne Kneuss wrote: >> On Thu, Mar 18, 2010 at 5:47 PM, mathieu.suen >> wrote: >>> Peter Lind wrote: >>>> >>>> On the contrary, it's quite obvious what's going on. In both examples >>>> __get() returns an array as PHP would normally do it (i.e. NOT by >>>> reference) which means that if you try to modify that you'll end up >>>> modifying nothing much. However, in your second example, the point at >>>> which you call __get() indirectly comes before the assign to the zork >>>> array - hence, the $this->zork['blah'] = 'blah'; no longer indirectly >>>> calls __get as object::$zork now exists. >>>> >>>> In other words, this is down to you confusing passing a variable by >>>> reference and passing it by value: PHP normally passes arrays by >>>> value, so when __get() returns an array, you're working on a copy of >>>> the array you returned. As someone noted earlier, you can easily >>>> change the behaviour of __get to return variables by reference, should >>>> you want to. However, I personally wouldn't want this to be default >>>> behaviour as that would make debugging apps much more annoying - >>>> things should be consistent, even if consistency at times confuse >>>> people. >>>> >>>> Regards >>>> Peter >>>> >>>> >> >>