Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:47394 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 29325 invoked from network); 18 Mar 2010 13:36:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Mar 2010 13:36:10 -0000 Authentication-Results: pb1.pair.com header.from=peter.e.lind@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=peter.e.lind@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 216.239.58.190 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: peter.e.lind@gmail.com X-Host-Fingerprint: 216.239.58.190 gv-out-0910.google.com Received: from [216.239.58.190] ([216.239.58.190:56414] helo=gv-out-0910.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 11/27-20429-84C22AB4 for ; Thu, 18 Mar 2010 08:36:08 -0500 Received: by gv-out-0910.google.com with SMTP id l14so5733gvf.37 for ; Thu, 18 Mar 2010 06:36:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=zjyl9S3q1zCOvCiECPqvWjoBMW0/FV9zICInwumoJBg=; b=UhAaGLIySrSOpm1pjVTtqCnN4Cx+xImtt9MrPEnhwuBbDHmbhbf3vTVhhQEwKpmuuk At9jZdW9eNHak1v4XsUjXaFqpmGodF6ld8VGfVf+7+8JdbbkKt3gUQl02BqGM6l1CyHH QvYGYGGq/cfIlAmSwf++0WT9j6i7XKi+62AgE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=ewcbOWEBO2AlrS8p4Gmp821vYItux2vSVv1zTor4aIzIBP35a0KfcGj26sc6NtZhlX X+HcMEXleeWoWbKTVCNHS8bjDa67DAuRIetGcCPadnBS9Osfe4U1IAOEgl+RNHcqh0dL 569YZK1Yw0SA63BADwE7sbSE2ONKaY6h24D8U= MIME-Version: 1.0 Received: by 10.102.13.18 with SMTP id 18mr2323535mum.40.1268904295250; Thu, 18 Mar 2010 02:24:55 -0700 (PDT) In-Reply-To: <4BA1DB19.1080608@easyflirt.com> References: <4BA0DF61.1010907@easyflirt.com> <4BA0E39C.7020600@gmail.com> <4BA0E9E8.8000404@easyflirt.com> <4BA1DB19.1080608@easyflirt.com> Date: Thu, 18 Mar 2010 10:24:35 +0100 Message-ID: <660eb66f1003180224g5662ba1dtb123c3ee88a3a3e5@mail.gmail.com> To: "mathieu.suen" Cc: Etienne Kneuss , internals@lists.php.net Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Assign array with __get From: peter.e.lind@gmail.com (Peter Lind) > I think there is a lot =C2=A0to say why is not working but just look at t= hose > 2 execution: > > ---------------- 1st > class A > { > > =C2=A0 =C2=A0 =C2=A0 =C2=A0public function __get($name) > =C2=A0 =C2=A0 =C2=A0 =C2=A0{ > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$this->$name =3D a= rray(); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return $this->$nam= e; > =C2=A0 =C2=A0 =C2=A0 =C2=A0} > > =C2=A0 =C2=A0 =C2=A0 =C2=A0public function test() > =C2=A0 =C2=A0 =C2=A0 =C2=A0{ > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$this->_zork; > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$this->_zork['bar'= ] =3D 67; > =C2=A0 =C2=A0 =C2=A0 =C2=A0} > } > > $a =3D new A; > $a->test(); > > var_dump($a); > ---------------- 2nd > class A > { > > =C2=A0 =C2=A0 =C2=A0 =C2=A0public function __get($name) > =C2=A0 =C2=A0 =C2=A0 =C2=A0{ > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$this->$name =3D a= rray(); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return $this->$nam= e; > =C2=A0 =C2=A0 =C2=A0 =C2=A0} > > =C2=A0 =C2=A0 =C2=A0 =C2=A0public function test() > =C2=A0 =C2=A0 =C2=A0 =C2=A0{ > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$this->_zork['bar'= ] =3D 67; > =C2=A0 =C2=A0 =C2=A0 =C2=A0} > } > > $a =3D new A; > $a->test(); > > var_dump($a); > ---------------- > > > Adding something that don't have side effect make the side effect > work.... (more or less) > You almost have to know how the VM is implemented in other to know what > is going on. > Nothing is obvious. > 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'] =3D '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 --=20 WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51