Developers/Object oriented programming in PHP/Objects/en

Object storage in PHP
PHP uses hash tables to store objects. These tables are structured in key/value pairs, so that a certain key in the key column maps to the object data in the value column.

When creating a new object and assigning it to a variable like  the following happens: Memory for the object is allocated, the constructor method is called to initialize the object and an identifier for this object is stored in the hash table which manages the object and references the memory block. This identifier is assigned as value to the variable $objBar, so that $objBar only contains a reference to the object, and not the actual object data itself.

Assign and copy objects
Now, if this $objBar variable is assigned to another variable $objFoo like

then the normal rule for assignments in PHP applies: The content of $objBar is copied and assigned to $objFoo. But the content of $objBar is just the identifier of the object, so this identifier is copied. As a consequence both variables $objBar and $objFoo</tt> are pointing now to the same and identical object. In other words, the object can be accessed using both $objBar</tt> and $objFoo</tt>. A so called shared resource is created and the object isn't copied.

Due to this, you can still modify the internal state of the object by calling it's public methods even after a reference of the object was already handed over to another object. This can be very efficient but also very dangerous, so it must be handled with care, because it can lead to errors which are hard to find. To actually copy an object in order to get a second object with the same property values the clone</tt> keyword must be used:

With clone</tt> all properties of the cloned object are copied to the new object. Properties containing scalar types or arrays are just copied when assigned, but if the properties of the cloned object contain identifiers to objects itself, then only these identifiers are copied. This results in two different objects with properties pointing to the same objects. To circumvent this behaviour and actually copy the objects hold by the properties of the cloned object, the magic __clone</tt> method must be implemented in the class definition of the cloned object. In this __clone</tt> method the objects referenced in its properties can be cloned by using the clone</tt> keyword again:

Problems using destructors
Destructors can be implemented to clean up before an object is actually deleted, which happens when the script ends or if there's no variable pointing to the object anymore when the garbage collector is running. If there's a __destruct</tt> method, it's executed before the object will be deleted, even if the script is terminated with.

When multiple class instances share the same resources and the destructor deletes the resources in one instance, then all other instances will loose their resources as well. So be carefully with shared resources and destructors.

Another problem arises if you try to access global resources not exclusively managed by the object itself in the destructor. Chances are high that the global resources are already deleted when the destructor of the object is called, which results in a fatal error when trying to call methods of the non-existing object.