Convert an array in an object instance

php.internals

Francisco M. Marzoa Alonso

21 years ago
Hi, Is it possible to convert an array in an object instance? I can convert an object in an array as follows: $Test = new MyClass (); $TestArray = (array) $Test; Then I've an array with all members of the object $Test, but it seems that I cannot simply do: $TestObject = (MyClass) $TestArray; To recover the object in the inverse way, so... is there any manner to do this? Thanks a lot in advance,

Derick Rethans

21 years ago
On Mon, 25 Oct 2004, Francisco M. Marzoa Alonso wrote:
> Hi, > > Is it possible to convert an array in an object instance?
Please use the php-general@lists.php.net mailinglist for user support questions, this is the wrong list. Derick
-- Derick Rethans http://derickrethans.nl | http://ez.no | http://xdebug.org

Francisco M. Marzoa Alonso

21 years ago
Sorry, I've ask this question on general but no one seems to know if there's a solution for it. In fact, its possible to do something as: $MyObject = new MyClass (); $MyArray = (array) $MyObject; $MyObject2 = (object) $MyArray; But the original class of the object is lost -$MyObject2 appears as an instance of stdClass instead MyClass-. This wouldn't be a problem if there were a manner to tell PHP that $MyObject2 is a MyClass instance after the last line, but I didn't find a way to do that. In Perl there's a function that's called 'bless' that servers for this purpouse: http://www.emerson.emory.edu/services/perl/perldoc/manual/perlfunc/bless.html But I didn't found an equivalent in PHP. I'm writting my own serialization routines and I need this to unserialize objects. Thx. Derick Rethans wrote: