Sending a PHP object to another PHP page

I'd like to know if there's a way by which a PHP object could be sent to another PHP page on a different machine?

For instance - I've implemented a class that constructs a Trie. Now after the Trie has been constructed , I'd like to send across the object to another PHP page so that it can access the object too.

Would probably packaging it into some sort of encoded JSON request and then sending it to a page which could relay it to the required page using jQuery , be a feasible option ?

I'm sorry I'm absolutely new to this !

Will Appreciate any help provided.

Cheers!

Answers


An object is an instance of a Class. Objects can't be sent around as they are. A "transmissible" object must be Serializable: make sure that your class implements Serializable

Once you've implemented the interface, just call the serialize and deserialize methods to get the object-string and to rebuild the object


Use php functions serialize() and unserialize(). When you unserialize the object, be sure to have its class defined.


personally I'd store the object in an object store rather than serialise memcache, APC even a session can be used, you can also use nosql style databases and key stores all of which are pretty much perfectly suited to object persistance as they're mostly very fast access data stores without the sql overhead. nosql-database.org

Just assign each object a key based off the key of the user browsing generate a new key for each new browser and store it in session/cookie to retrieve their personal "objects"

As already mentioned by STT you can serialise and store in the session thats perfectly fine although serialise is retarded to implement in php they should never have put that in.

Instead look at APC and SPL both are built in to php (APC is more suited for an object store especially since from 5.6 (I believe so onwards) its no longer an extension but built into the PHP core its self making it fully native so you get not only a simple object store but also op code cache which will seriously increase the speed of your php pages.

Note: APC is only really usable for object store when you run a single web / php server if you need multiple processing servers then you'll need a distributed object store in which case the best you can probably get is memcache

Lots of links mostly all on SO.

Using Memcache vs Memcached

Memcached vs APC which one should I choose?

http://php.net/manual/en/book.memcached.php

http://php.net/manual/en/book.apc.php

Is there a way in PHP to use persistent data as in Java EE? (sharing objects between PHP threads) without session nor cache/DB


The right way is to implement Serializable interface in the class of your object (if it doesn't yet) that pass it through some sort of transport between two servers.

Try to not send data trough client-side code unless you trust client or you don't care about data.


Need Your Help

KeyListener wont focus

java swing focus keylistener

I'm new to Java so please forgive me asking for help, but after 4 hours of searching for a solution I'm really getting desperate.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.