__clone in PHP 5



When using OO PHP there sometimes becomes the need to duplicate an object. If a new object has been instantiated and methods have been used to populate its properties, there may be a need to create a duplicate of the current version of that object for separate usage.

Example __clone Usage

In the example below the ‘WebDeveloper’ object is made and the name is set using the __set magic method.

A clone is then made of the ‘WebDeveloper‘ object and saved into its own variable (when ‘clone’ is used the ‘__clone’ is automatically called within the class). The __set method is then used to reset the ‘name‘ property to something different, in this case ‘Jordizle’.


Visitor stats.