# Object custom serializing infrastructure

- Thread: https://externals.io/message/15168
- Original archive: https://news-web.php.net/php.internals/15168
- Started by Stanislav Malyshev on 2005-02-23T12:02:45+00:00
- Messages: 2
- 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

---

## #15168 — Stanislav Malyshev — 2005-02-23T12:02:45+00:00

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

I have added the callbacks to class_entry structure and patches to 
serialization mechanism to allow custom object 
serialization/unserialization inside the PHP serialize mechanism.

Purpose:
--------
                                                                                                            
Define possibility for custom serialization and userialization of PHP5
objects, which would allow users to run custom code when objects are
serialized and unserialized.
                                                                                                            
Implementation:
---------------
                                                                                                            
Define 2 callbacks for zend_class_entry, as follows:
                                                                                                            
int (*serialize)(zval *object, unsigned char **buffer, zend_uint *buf_len, 
zend_serialize_data *data TSRMLS_DC);

Serializes object data into the buffer. Should serialize only object
"internals", not class name.
zend_serialize_data is the variables hash that var.c uses to track 
references. If you want to use it, consult var.c and use it the same way. 
                                                                                                            
int (*unserialize)(zval **object, const unsigned char *buf, 
zend_uint buf_len, zend_unserialize_data *data TSRMLS_DC);

creates new zval according to the serialized data in buf, 
buf_len limits data length. Returns SUCCESS if object
created correctly, FAILURE if it failed. Data include only the object 
data, not class name, etc.

zend_unserialize_data is the variables hash that var_unserialize.re uses 
to track references. If you want to use it, consult var.c and use it the 
same way.

By default, these callbacks are NULL, meaning the standard mechanism is 
used.

Format
------

New serialization is using letter 'C' to designate custom serialization 
data. 

Example
-------

Attached in the example patch for SimpleXML that demonstrates the concept 
by using callbacks to serialize SimpleXML object's data as XML and then 
restoring it to the object back. This should allow, for example, to 
seamlessly put SimpleXML object to a session data.



-- 
Stanislav Malyshev, Zend Products Engineer   
stas@zend.com  http://www.zend.com/ +972-3-6139665 ext.115

---

## #15169 — l0t3k — 2005-02-23T21:03:13+00:00

URL: https://externals.io/message/15168#15169 · In reply to: #15168

Stas,
    can the serialized representation contain high-bit/control chars or 
would we need to base64 encode or similar in that case ?

l0t3k
"Stanislav Malyshev" <stas@zend.com> wrote in message 
news:Pine.LNX.4.58.0502231318140.32475@shire.zend.office...
>I have added the callbacks to class_entry structure and patches to
> serialization mechanism to allow custom object
> serialization/unserialization inside the PHP serialize mechanism.
