Specifications
However, you might still want to store a PHP array or object in a file or database. If you do,
there are two functions you need to know how to use: serialize() and unserialize().
You can call the serialize() function as follows:
$serial_object = serialize($my_object);
If you want to know what the serialization actually does, take a look at what is returned from
serialize. It turns the contents of an object or array into a string.
For example, we can look at the output of running serialize on a simple employee object,
defined and instantiated thus:
class employee
{
var $name;
var $employee_id;
};
$this_emp = new employee;
$this_emp->name = “Fred”;
$this_emp->employee_id = 5324;
If we serialize this and echo it to the browser, the output is
O:8:”employee”:2:{s:4:”name”;s:4:”Fred”;s:11:”employee_id”;i:5324;}
It is fairly easy to see the relationship between the original object data and the serialized data.
As the serialized data is just text, you can write it to a database or whatever you like. Be aware
that you should addslashes() to any data before writing it to a database, as per usual. You can
see the need for this by noting the quotes in the previous serialized string.
To get the object back, call unserialize():
$new_object = unserialize($serial_object);
Obviously, if you called addslashes() before putting the object into a database, you will need
to call stripslashes() before unserializing the string.
Getting Information About the PHP Environment
A number of functions can be used to find out information about how PHP is configured.
Finding Out What Extensions Are Loaded
You can easily see what function sets are available, and what functions are available in each of
those sets using the get_loaded_extensions() and get_extension_funcs() functions.
Other Useful Features
C
HAPTER 21
21
OTHER USEFUL
FEATURES
451
26 7842 CH21 3/6/01 3:41 PM Page 451










