Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using __clone() 1

Status
Not open for further replies.

kyru

Programmer
Jan 17, 2009
45
ES
Anyone knows how is this function used? It's supposed to create an identical copy of an existing and it's also supposed that the function doesn't have to be declared on it's corresponding class.

But in real I do the following thing and it doesn't work:

$first=new Prueba2("9","2","3","4");
$second=$first->__clone();

In fact, even declarating a __clone() function into the class Prueba2, it doesn't work.

I'm using last PHP version (5.2.8), in the case that could influence.

Thanks in advance.
 
you clone an object just by doing this

Code:
$newObj = clone $oldObj;

the __clone method of the class is only instantiated if it exists in the class definition for $oldObj. if there is no method specified then the object will simply be cloned.

beware that cloning using clone may not be quite what you want in each case. references, for example, are maintained.

By contrast, if you want to do a deep clone (breaking references but maintain property values) then you might try this

Code:
$newObj = unserialize(serialize($oldObj));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top