2

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!

Hormigas
  • 1,429
  • 5
  • 24
  • 45
  • 5
    Make sure your class implements `Serializable` interface, then serialize the object, send it as a string, then unserialize it. – STT LCU Dec 09 '13 at 15:58
  • personally I'd store the object in an object store rather than serialise and post but thats just me :) – Dave Dec 09 '13 at 16:02
  • @dave object stores doesn't live well when you need to "change" page – STT LCU Dec 09 '13 at 16:20
  • @STTLCU yep they do. memcache, APC hell even a session can be used. Most of the nosql databases are suited for object store 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" http://nosql-database.org/ – Dave Dec 09 '13 at 16:23
  • @Dave I was trying to keep it simple using only vanilla PHP (well, even memcache is quite an advanced topic). I agree completely with you, just note that it's expressively prohibited to store objects inside a session variable. – STT LCU Dec 09 '13 at 16:25
  • @sttlcu as you already mentioned 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) – Dave Dec 09 '13 at 16:28
  • Lots of links mostly all on SO. http://stackoverflow.com/questions/1442411/using-memcache-vs-memcached http://stackoverflow.com/questions/815041/memcached-vs-apc-which-one-should-i-choose http://php.net/manual/en/book.memcached.php http://php.net/manual/en/book.apc.php http://stackoverflow.com/questions/15086349/is-there-a-way-in-php-to-use-persistent-data-as-in-java-ee-sharing-objects-bet – Dave Dec 09 '13 at 16:29
  • Why not turn all your comments into an answer? I'm sure that your advice will be very valuable for more advanced users of this community (including me, I'll go reading about APC straight away, thank you @dave) – STT LCU Dec 09 '13 at 16:30
  • Good point :) will do that now – Dave Dec 09 '13 at 16:31

4 Answers4

6

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

STT LCU
  • 4,348
  • 4
  • 29
  • 47
1

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

Ghigo
  • 2,312
  • 1
  • 18
  • 19
1

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.

When should I use Memcache instead of 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

Community
  • 1
  • 1
Dave
  • 3,280
  • 2
  • 22
  • 40
0

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.

Andrew
  • 1,756
  • 3
  • 18
  • 31