I have a class a and instantiated it using new
$obja = new a;
I know the difference between the below two lines
$obja2 = $ojba;
$obja2 = clone $obja;
But even if you declare or not declare __clone in the class a the first line $obja2 refer to $obja memory space and second line creates a copy of the $obja. Till here it is clear to me.
Then why php is having a magic method __clone? Is it only for executing a set of codes which is written inside __clone while we use $obja2 = clone $obja;
Somebody please help to get a better idea of it.
Awith a property$innerB, which contains an instance of the classB. When you clone the instance ofA, the new object will have the same instance ofB. By using clone, you could do$this->innerB = clone $this->innerB;- so you get a new instance ofBfor the new object as well. Consider this vs this.