Skip to main content

Object Cloning in PHP

In simple words 'object cloning' can be defined as copying the object property and assigning to a different object. But when we copy an object then it only owns the reference, not the value.

In PHP this is done by two method-

1) By using clone Keyword-
In this, a shallow copy of the object is made. It means the internal object of the objectA is not cloned to the internal object of objectB.


class CloneableClass { private $_internalObject; public function __construct() { // instantiate the internal member $this->_internalObject = new stdClass(); } } $objectA = new CloneableClass(); $objectB = clone $objectA;












2) By using __clone() method-

if your object holds a reference to another object which it uses and when you replicate the parent object you want to create a new instance of this other object so that the replica has its own separate copy.
In this the magic method __clone() is used to clone internal object or the referenced one. So objectB will be the exact copy of the objectA.


class CloneableClass { private $_internalObject; public function __construct() { // instantiate the internal member $this->_internalObject = new stdClass(); } // on clone, make a deep copy of this object by cloning internal member; public function __clone() { $this->_internalObject = clone $this->_internalObject; } } $objectA = new CloneableClass(); $objectB = clone $objectA;

Comments

Popular posts from this blog

Code for Mail in PHP

PHP mail function and mail configuration in  XAMPP  and sending mail is done from sendmail through localhost. I hope it will help you. mail() function <? php $to = 'dubeynitish22@hotmail.com' ; $subject = 'Test' ; $message = 'Hello' ; $headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion ();   if (! mail ( $to , $subject , $message , $headers )){ echo "Error !!" ; } else { echo "Email Sent !!" ; } ?> 2. php.ini configuration (For SEND-MAIL) [ mail function ] ; For Win32 only . ; http : //php.net/smtp ; SMTP = localhost ; http : //php.net/smtp-port ; smtp_port = 25   ; For Win32 only . ; http : //php.net/sendmail-from ; sendmail_from = me@example . com   ; For Unix only . You may supply arguments as well ( default : "sendmail -t -i...

Magic Function in PHP (__sleep() and __wakeup() )

There are many magic methods in PHP like  __construct(), __destruct(), __callback(), __get(), __set(), __sleep(), __wake() and many more. But we will be takingon  on  __sleep() and  __wake(). __sleep() : serialize() checks if your class has a function with the magic name __sleep(). If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return anything then NULL is serialized and E_NOTICE is issued. serialize() is used for the representation of the storage class for storing the value. Serializing   an object means converting it to a byte stream representation that can be stored in a file. The use of __sleep()  to commit the pending task. If a bulk data is being inserted then at that time __sleep can be used. it will not release the object unless the work is not completed....

PHP code to find the diameter of tree in O(n) time complexity

<?php class BinaryNode{ public $value = null; // node value public $left = null; // left child public $right = null; // right child public function __construct($value) { $this->value = $value; } } class BinaryTreeDiameter{ //find the diameter and print its value public function findDiameterOfTree($root){ //Checking node is empty or not if($root === null){ return 0; } // Compute the depth of each subtree $lDepth = $this->findDiameterOfTree($root->left); $rDepth = $this->findDiameterOfTree($root->right); // Return the greater one. if ($lDepth > $rDepth) return $lDepth+1; else return $rDepth+1; } //find the diameter and print its ...