# Convert an array in an object instance

- Thread: https://externals.io/message/13500
- Original archive: https://news-web.php.net/php.internals/13500
- Started by Francisco M. Marzoa Alonso on 2004-10-25T11:31:20+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #13500 — Francisco M. Marzoa Alonso — 2004-10-25T11:31:20+00:00

URL: https://externals.io/message/13500#13500

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,

---

## #13501 — Derick Rethans — 2004-10-25T12:04:46+00:00

URL: https://externals.io/message/13500#13501 · In reply to: #13500

> 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

---

## #13516 — Francisco M. Marzoa Alonso — 2004-10-26T07:11:52+00:00

URL: https://externals.io/message/13500#13516 · In reply to: #13501

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:

>
>
>  
>
> > […]
>
>Please use the php-general@lists.php.net mailinglist for user support
>questions, this is the wrong list.
>
>Derick
