FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » out of sheer curiosity...
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: out of sheer curiosity... [message #177610 is a reply to message #177600] Tue, 10 April 2012 21:08 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
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:
> <?php
> 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.

>> A more intelligent approach may have been to define an interface which any "result
>> row" class must implement and which defines a method like setData( $rowAsArray ). Or
>> maybe even better: make use of the existing __set_state() magic method.
>
> The rationale (or use case) of __set_state() is a mystery to me.
>
> Maybe they did not want to ask too much from PHP programmers...
>
> /Str.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: 5.4 windows installer.
Next Topic: Does PHP5 treat $_SERVER['PHP_AUTH_USER']) differently?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 22 18:28:28 GMT 2024

Total time taken to generate the page: 0.04621 seconds