Re: [PECL-DEV] Re: shared memory extention

[email protected] (Johannes Schlüter) Wed, 14 Apr 2010 14:57:13 +0200
Newsgroups php.pecl.dev
Organization php.net
Message-ID <1271249833.9207.22.camel@guybrush>
Hi,

On Wed, 2010-04-14 at 22:06 +1000, Geoffrey McRae wrote:
> No Problems, I dont really care what licence it lives under. I am
> re-working some of its code base to add array support, as for object
> support, if I can figure out how to serialize the object in the
> extension then I will do so.
> 
> Can I just call __serialize on the object?

No. (well, yes you could, but it makes little sense as general approach)

Sample code to serialize a zval:

#include "ext/standard/smart_str.h"
#include "ext/standard/php_var.h"

zval *data; /* data to be serialized */
php_serialize_data_t var_hash; /* temporary data */
smart_str buf = {0}; /* result will be stored in there */

PHP_VAR_SERIALIZE_INIT(var_hash);
php_var_serialize(&buf, &data, &var_hash TSRMLS_CC);
PHP_VAR_SERIALIZE_DESTROY(var_hash);


buf.c then has the data as char*, buf.len the length. In case of a
problem buf.c will be NULL.

The var_hash thing is needed as the serializer supports serializing
different related zvals in sequence. (This was needed for sessions and
register_globals ...)

Serializing looks like that:

unsigned char *data; /* serialized data */
int data_len; /* length of the serialized data */
php_unserialize_data_t var_hash;

PHP_VAR_UNSERIALIZE_INIT(var_hash);
if (!php_var_unserialize(&return_value, &data, data + data_len, &var_hash TSRMLS_CC)) {
	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
        /* error handling */
} else {
	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
	/* success, return_value is filled */
}

You can also check the implementation of (un)serialize in ext/standard,
this was from some other code I had at hand right now.

hope it helps

johannes