Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!nuzba.szn.dk!pnx.dk!fu-berlin.de!uni-berlin.de!not-for-mail From: "M. Strobel" Newsgroups: comp.lang.php Subject: Re: out of sheer curiosity... Date: Wed, 11 Apr 2012 10:40:26 +0200 Lines: 105 Message-ID: <9ukubqFtk8U1@mid.uni-berlin.de> References: <17777949.TfjZNtg3zH@PointedEars.de> <9tij5fFmi8U1@mid.uni-berlin.de> <4f74567c$0$6887$e4fe514c@news2.news.xs4all.nl> <4f746ff9$0$6918$e4fe514c@news2.news.xs4all.nl> <9uebvuFt0pU1@mid.uni-berlin.de> <9ueidtFagfU1@mid.uni-berlin.de> <9ufofaFvj5U1@mid.uni-berlin.de> <9ui7u7FkvdU1@mid.uni-berlin.de> <9uj6u0F2j6U1@mid.uni-berlin.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 01cfW++/h6PohXxiSmFxSQEAh7xjrAduRbjdquUtpDJ3/8fMo= User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; de; rv:1.9.2.28) Gecko/20120306 Thunderbird/3.1.20 In-Reply-To: Xref: csiph.com comp.lang.php:5455 Am 10.04.2012 23:08, schrieb Jerry Stuckle: > On 4/10/2012 12:54 PM, M. Strobel wrote: >> Am 10.04.2012 13:28, schrieb Thomas Mlynarczyk: >>> M. Strobel schrieb: >>> >>>> Now PDO comes in. When getting a query result as object your constructor is called >>>> *after* the properties are set. >>> >>> Really? That would be very strange indeed. >>> >>> [minutes later] >>> I just tested it and it is indeed as you state. Thanks for pointing that out -- is >>> this documented? >>> >>>> I found it quite annoying to have an exception from the rule. But thinking more >>>> about >>>> it I found out they had a choice of either calling __construct() after setting the >>>> data, or to define another magic method like __pdoconstruct() to give the programmer >>>> a chance to adapt the init process to the data. >>> >>> I don't quite follow you here. Why did they have no choice? I don't see why they >>> (c|sh)ould not call the constructor before setting the data. After all, that would be >>> the only logical and sensible thing to do. If you would do this "manually", you would >>> do it like this: >>> >>> foreach ( $rows as $row ): >>> $item = new MyClass( 'foo', 'bar' ); >>> foreach ( $row as $name => $value ): >>> $item->$name = $value; >>> endforeach; >>> $result[] = $item; >>> endforeach; >>> >> >> Meanwhile I have more ideas about choices. >> >> The correct way would have been to pass the row to the constructor. This would have >> required a matching parameter definition for the constructor, and the corresponding >> code, quite error prone. >> > > Yes, that's how C++ does it, for instance (overloaded functions). It would be harder > in PHP, but it could be done. > >> In your example above the problem is if you need to do more init work on the data you >> need an extra method call. With the implemented solution you just have to know that >> the constructor is called after setting the vars, and you can detect it, testing the >> unique key or so, but need not do so if you don't care. >> >>> And I guess it would be impossible to do it the other way round (first setting data, >>> then calling constructor). The setting of the data can be controlled using the magic >>> __set() method, so there's no need for __pdoconstruct(), if I understand you >>> correctly. >> >> Are you aware of the fact that __set() is not called for defined variables? Test: >> > class Upset { >> public $var1; >> function __set($n, $v) { >> echo "setting variable $n to: $v\n"; >> $this->$n = $v; >> } >> } >> $c = new Upset(); >> $c->var1 = 'Try this'; >> $c->another = ' and this '; >> echo "Values are now $c->var1 $c->another\n"; >> ------------------- Result: ---- >> setting variable another to: and this >> Values are now Try this and this >> >> > > That is correct - you are directly setting a public variable. Now make $var1 private > and see what happens. Let's see, this gives a good reference: $n = $v; } function __get($n) { echo "getting variable $n\n"; if (isset($this->$n)) return $this->$n; else return null; } } $c = new Upset(); $c->pubvar = 'Try this'; $c->privvar = ' private '; $c->adhoc = ' and this '; echo "Values are now $c->pubvar $c->privvar $c->adhoc\n"; ------------------- Result is: ---- setting variable privvar to: private setting variable adhoc to: and this getting variable privvar Values are now Try this private and this /Str.