Newbie question: un/serialize case for PHP Objects

9 views
Skip to first unread message

Stuart Moffatt

unread,
Feb 25, 2010, 9:48:09 PM2/25/10
to php.js
In php.js, serialize has a code block where the case is 'object', but
has no case for 'o' (PHP object).
Are there plans to support serialization of PHP objects to JS
prototypes (and vice-versa)?

For instance, I'd like to be able to do this:

--- php ---
class Person
{
private name
private child

... setters and getters ...
}
$person = new Person()
$person->setName("Bob Smith")
$child = new Person()
$child->setName("Bobby Smith, Jr")
$person->setChild($child)

$serialized_person = serialize($person)
-- php --

... pass $serialized_person to client via http ...

-- js --
... include serialize.js from php.js ...
var serialized_person = '<?php echo $serialized_person ?>';
// and unserialize in js
var person = unserialize(serialized_person)
document.write('person name: ' + person.getName())
document.write('child name: ' + person.getChild().getName())
-- js --

Are people already doing it this way? Any help / hints would be good.


Thanks,
Stuart


Brett Zamir

unread,
Feb 25, 2010, 10:30:23 PM2/25/10
to ph...@googlegroups.com, Stuart Moffatt
I think what you want is json_encode() and json_decode(), available in
php.js as well as PHP... JSON is just a subset of JavaScript to
represent serialized data. I don't know if there are any differences
between what information is preserved in serialize() and in
json_encode(), but I think json_encode() should also preserve the object
information...

Brett

Kevin van Zonneveld

unread,
Feb 26, 2010, 6:39:24 AM2/26/10
to ph...@googlegroups.com, Stuart Moffatt
But you can't transfer a ->getName() php method, and expect it to work in js.

--
Met vriendelijke groet / Kind regards,

Kevin van Zonneveld
http://kevin.vanzonneveld.net

http://twitter.com/kvz

Stuart Moffatt

unread,
Feb 26, 2010, 8:32:48 AM2/26/10
to Kevin van Zonneveld, Brett Zamir, ph...@googlegroups.com
@Brett - yes, I am aware of the json en/decode utilities, but when I unserialize I don't want JSON objects -- I want my javascript prototypes on the client-side and my PHP classes on the server-side. However, the unserialize method in php.js does not have a  case for the PHP-serialized type of 'O' (meaning PHP object). There is another library that demonstrates this is possible to do with objects (PHP_Unserialize), so my question was why this is not implemented in php.js

@Kevin With reflection in PHP I can generate static JS prototypes so that I maintain only one codebase (in PHP) for my domain model. I am pretty sure that means I won't have to transfer my setter/getter functions along with the serialized object, so long as I can 'cast' my unserialized data into a my own prototype (working on how to do this).

I am currently working on a new case for unserialize in php.js that will properly parse the serialized object string and let the other cases (i,b,d,n,s,a) do the heavy lifting for object properties. 

I was hoping that someone more familiar with php.js has already implemented this, or has a conventional workaround for passing objects rather than other data structures.

Stuart

Kevin van Zonneveld

unread,
Mar 24, 2010, 6:44:12 AM3/24/10
to ph...@googlegroups.com, Brett Zamir
Hey Stuart,

I'd be interested to see some code samples of what you are trying to achieve.
Let me know how it goes in any case!

Reply all
Reply to author
Forward
0 new messages