Before starting this article/tutorial you should already be familiar with PHP, and basic class and object syntax. This article is going to go through some useful operations you can do with objects. The first thing I want to demonstrate is how to store objects and their member variables.

class MyCart{

	var $items;

	function add_item ($artnr, $num){

		$this->items[$artnr] += $num;

	}

}

session_start();

$_SESSION['user_cart'] = new MyCart();

In this case we are storing the object in the session variable. It is a fairly painless process, and getting that stored object on the next page the user loads is just as easy, after session_start() it will be available just like a session variable. If you try to use the session stored object on a page that does not have the class’s declaration, class MyCart{}, you will get a generic object with no member functions, as explained on the PHP manual page ‘Object Serialization’.

$user_cart = new MyCart();

$user_cart_string_data = serialize($user_cart);

$fp = fopen('data.txt', 'w');

fwrite($fp, $user_cart_string_data);

fclose($fp);

This is another basic method of storing objects. We converted the object to a string representation, and made or overwrote a file with that data. If it is not already clear, you restore the object by reading the file into a string, then using unserialize on it, reversing the effects of serialize. Like the session type of storage, you need the class to be defined in any file that uses that type of object.

$user_cart = new MyCart();

$user_cart_string_data = mysql_real_escape_string(serialize($user_cart));

mysql_query("INSERT INTO objects (objectdata) VALUES ('$user_cart_string_data')");

The third example is just to show it in a database context instead of a file. Usually you need to escape the serialized variable whether it is an object or array, and that is why mysql_real_escape_string is used here, but there are other ways to handle these things depending on the situation.

Here are a few final notes on these types of storage. Objects can have references in their member variables, but I recommend not relying on them when an object is loaded from stored data. You can set up your objects to set these references again, with an initialize function, or find other ways of dealing with them. If you do try to use the references in these objects various things can happen, using $_SESSION it is actually possible to refer to other $_SESSION variables. There are some rare data types as well as references that can cause problems when storing objects, but that is beyond the scope of this article.

$user_cart = new MyCart();
$user_cart->add_item("cat", 2);
$vars = get_object_vars($user_cart); //$vars = Array( [items] => Array ( [cat] => 2 ) )
$stdobj_mycart = (object)$vars; //this is an object now, but not a MyCart object

This is not something that is often used, but it can be useful to know how to get an array of an object’s member variables. Just use get_object_vars(). At the end, $stdobj_mycart tries to become an object by converting $vars. The result is an object with no class, or standard class.

Hopefully your head isn’t spinning from the various options for working with and storing objects. There is always a trade off in doing something that converts or stores critical and complex parts of a program, but hopefully this article has helped you get the most out of it.