Re: Re: Convert an array in an object instance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Francisco M. Marzoa Alonso wrote:
tularis@xxxxxxxxxxxx wrote:

No. You need to remember that in most cases where you cast a variable from
one type to another, you will experience data (precision) loss. This is
also the case in converting an object to an array. Only the properties are
copied over (since functions can not be part of an array).

The point is interesting, but as far as I do not need to store class methods I think there's nothing lost between conversion. I'll store the name of the class of the instance, so the methods of the instance will be those of that class. I only need some manner to tell PHP that the object is an instance of "MyClass" (following the original example) to have the object exactly equal as it was before conversions.


Perl has a function to to that, that's called bless:
http://www.perldoc.com/perl5.6/pod/func/bless.html

FYI, the reason I need this is because I'm creating my own object's serialization routines.


$obj = new MyClass; foreach($array as $key => $val) $obj->$key = $val;

Not very elegant but the only way as far as I know

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux