Is marshaling/ serialization in PHP as simple as serialize($var)? - php

here's a definition of marshaling from Wikipedia:
In computer science, marshalling
(similar to serialization) is the
process of transforming the memory
representation of an object to a data
format suitable for storage or
transmission. It is typically used
when data must be moved between
different parts of a computer program
or from one program to another.
I have always done data serialization in php via its serialize function, usually on objects or arrays. But how is wikipedia's definition of marshaling/serialization takes place in this serizalize() function?

What serialize doesn't do is transport class definitions. When unserializing an object, that object's class definition must be present (loaded from the code base), otherwise unserializing will fail. From the Wikipedia article you mention:
To "marshal" an object means to record its state and codebase(s) in such a way that when the marshalled object is "unmarshalled", a copy of the original object is obtained, possibly by automatically loading the class definitions of the object. You can marshal any object that is serializable or remote. Marshalling is like serialization, except marshalling also records codebases. Marshalling is different from serialization in that marshalling treats remote objects specially.
If I understand correctly, Serialize is definitely not 100% compatible with the definition of marshaling in that respect. I don't know a pre-defined mechanism that would do this in PHP. I guess you would have to combine the serialized data and all necessary class definitions into a package (a ZIP file for example).

Like Pekka mentioned above, PHP doesn't include the class definition, so it does not do marshaling. If the class for a serialized object is present, however, then the answer to your question is yes: serialization is as easy as serialize($abc).
The best way that I know of to take care of marshaling in PHP is to use a third party tool like Google Buffer Protocols or Facebook (Apache?) Thrift, which will serialize and marshal for you. Kind of a roundabout way of doing it (and as long as you have the class present, you don't need to marshal anyway), but they're probably the best solution to the problem.

Related

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!
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.
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
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.

Maintain XML Document state after session serialization

The class instance that I want to store in session holds an array of loaded DOMDocuments.
As noted in one of the answers here: PHP quirks and pitfalls, when you serialize an object containing XML, the XML structure does not survive the unserialize process. As I understand it PHP5 is supposed to automatically serialize session data, so what I need to know is how to make XML survive the serialize/unserialize process?
I've read about and it looks like it can't be done, plus the overhead involved in writing and reading the session file with the automatic serializing/deserializing seems to make it preferable to just read and write the XML files in the class instance on __sleep and __wakeup. Is that the case?
http://php.net/manual/en/function.serialize.php
This is useful for storing or passing PHP values around without losing their type and structure.`
The value to be serialized. serialize() handles all types, except the resource-type. You can even serialize() arrays that contain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other reference will be lost.
what is php resources
list of php resources
perhaps you can consider to store the original data into memcache, database,
while your session is pointing to that (like memcache key, database row ID)
additional read-up
you might felt amuse for the following (maybe i was wrong) -
http://www.php.net/manual/en/simplexml.resources.php
http://www.php.net/manual/en/dom.resources.php
http://www.php.net/manual/en/session.resources.php
Why don't you simply export the DOMDocument as a string, then serialize this string?

Should I store my PHP5 Objects in session for SPEED, and why?

As you know, when you store a class defination in SESSION serialized automatically, and are unserialized on each following pages.
I just started to write classes and I wonder that:
to store a class in session or a file with serializing is a good idea?
If yes, how can I STORE and then GET to use a class in PHP5?
You don't store a class in a session variable, but you can store an object. Take note that if your object has properties referring to resources like file handles and database connections, no amount of unserializing will bring them back.
Unless it's a tiny class, probably not (see this question for possible pitfalls with large sessions). In short, sessions are not designed to be a caching mechanism, and they don't perform too well when you make them into one.
Note that if you are using the default session handler, your sessions are stored on the hard drive - not very fast when you get many concurrent requests. Also (test and measure), serialization/deserialization may be slower than the normal methods of object creation - note that you'd probably be deserializing twice: from session to string, then string into object of that class.
If you want to go the serialization/deserialization route, try e.g. Memcached instead.
Storing object instances in the session has the following disadvantages:
Performance overhead: Even if you don't need some objects, the will be unserialized and instatiated on every request.
Strange bugs in development: Whenever you add or remove a property from an object, the instance from the session will not match the object definition.
Security: Typically the session data is stored separately from your application. Sometimes this location is not as access-protected and secure as the rest of your files.
Data duplication and wrong state: With sessions you may store the same objects over and over again for different users. Compared to a dedicated object cache, where each object is only stored once, this leads to increased storage needs and the possibility that an object has the wrong state because the state was changed in another session.
I'd rather store the objects in a dedicated cache. Have a look at the Zend Cache class as an example of a good cache library.
If your object uses resources (database connections, files, gd images) your class should implement the Serializable interface. You then have to add two methods that do cleanup and initialization stuff.

Ruby equivalent to PHP's "unserialize" function?

I'm working with some serialized data from a MySQL database and I need to deserialize this using Ruby (the serialized data is used to build up a WHERE clause for a database query). PHP has the unserialize() method which will convert it into an Array; what is the Ruby equivalent of this?
The data in question looks like this, if it helps any:
a:2:{s:5:"Lists";a:1:{i:0;s:2:"11";}s:5:"Rules";a:1:{i:0;a:3:{s:4:"type";s:5:"group";s:9:"connector";s:3:"and";s:5:"rules";a:1:{i:0;a:3:{s:4:"type";s:4:"rule";s:9:"connector";s:3:"and";s:5:"rules";a:3:{s:8:"ruleName";s:2:"21";s:12:"ruleOperator";s:10:"isnotempty";s:10:"ruleValues";a:1:{i:0;s:0:"";}}}}}}}
I guess the exact equivalent would be this. You could also check out Ruby's Marshall Class, specifically Marshal.load.
Look at Ruby's Marshal Class.
From the docs:
The marshaling library converts
collections of Ruby objects into a
byte stream, allowing them to be
stored outside the currently active
script. This data may subsequently be
read and the original objects
reconstituted. Marshaled data has
major and minor version numbers stored
along with the object information.
Of course this is a two way street, you can only un-Marshal, Marshaled ruby objects.
If it's XML, there's the Hash.from_xml method.

Method to Serialize Objects from appserver to web server without code duplication

I have a web-app-database 3 tier server setup. Web requests data from app, and app gets its data from the db then processes and returns it to web for display. Standard.
Currently all the data I serialize from app to web gets put into custom defined data structs that the web side then parses for display. In other words, say I have an order that I want to retrieve and send to web. I don't serialize the whole object, but rather build an array with the appropriate data elements while on the app server, then serialize that array over to the web servers.
Now, I am looking into serializing the entire order object over to the web server.
What is the best method you guys have found to serialize objects while maintaining separation of appserver and webserver code? I don't want my webservers having any code which accesses the DB, but I want them to reuse the classes that encapsulate my order and other data as much as possible.
To further refine my question (thanks to Glenn's response), I don't want my webservers to have any of the business logic around say the order class either, that only the appserver needs to know. The objects already use separate serialization to/from the database, and to/from the webservers.
Using the order example, on the appserver, an order should be able to cancel: ie
$order->cancel();
but on the webserver that method should not even be found. It shouldn't simply proxy back (directly) to the appserver order's cancel method, because user action requests should flow through the application's authorization and permissions checking layer.
So how do I get an order object on my webserver that has some (but not all) of the methods and properties of the object on my appserver, with little to no code duplication. One thing I've been thinking is to create a base class with a limited set of properties and methods. It would use an inner class to hold its properties, and I would require all data access to pass in and out of getters and setters. These would in turn call the getters and setters on the inner class.
The web and app servers could then independently extend the base class, and use a custom inner class to hold the properties. Then on the app side for instance, the inner class could be an ORM class extention that can persist data to the DB, and on the web side the inner class could be a simple properties holder class.
But the whole inner class thing seems a little clunky, so I'm still looking for better solutions.
Factor out format specific
serialization code into separate
classes using the Adapter
pattern. Your problem domain
classes become backing store
neutral.
Use relational database specific adapter classes on the app tier to serialize objects to and from the data tier.
Use HTML specific adapter classes on the web tier to serialize objects to and from the web browser.
Use XML (or whatever wire protocol friendly format you deem most appropriate) specific adapter classes on both the web and app tiers to serialize the objects between the web tier and the app tier.
You get extra points if you are clever enough to figure out how to make these adapter classes generic enough such that you don't need a different set of adapter classes per problem domain class.
If I understand your question correctly, you want to serialize the data of your objects, but when they are re-hydrated, they should be so into a different type of object (With limited and/or different functionality)?
You can do this in different ways, depending on what protocol you prefer. For example, you could use SOAP. You should then hydrate the objects into a different class on the client-side, than on the server-side. You could also use PHP's native serialization and either a) Have a different code base on the client (Could get confusing) or b) mock a bit around with the serialized string (Eg. replace the class-name). A crude example can be found in the comments for the PHP manual.

Categories