Can someone explain me
what is Object Cloning in php?
When should i use clone keyword in php?
Can someone explain me
what is Object Cloning in php?
When should i use clone keyword in php?
There might be a semicolon or bracket missing a line before your pasted line.
It seems fine to me; every string is allowed as an array index.
You could add a __clone()
method to your email class. Which is automatically called when an instance of this class is cloned via clone(). In this method you can then manually add the template.
Example:
class email {
__clone() {
$this->template = new template();
}
}
.
unserialize(serialize($object)); // would be another solution...
To make things short, an exception is a "special condition that change the normal flow of program execution" (quoting wikipedia)
You might be interested by (at least) those couple of articles :
They should give you some interesting elements -- especially the second one, for "what is an exception in php"
One of the advantages (which is part of the basic idea) is :
try
blockcatch
blockedit: it's currently 2016-09-24, and PHP 5.4 has been released 2012-03-01, and support has ended 2015-09-01. Still, this answer seems to gain upvotes. If you're still using PHP < 5.4, your are creating a security risk and endagering your project. If you have no compelling reasons to stay at <5.4, or even already use version >= 5.4, do not use this answer, and just use PHP>= 5.4 (or, you know, a recent one) and implement the JsonSerializable interface
You would define a function, for instance named getJsonData();
, which would return either an array, stdClass
object, or some other object with visible parameters rather then private/protected ones, and do a json_encode($data->getJsonData());
. In essence, implement the function from 5.4, but call it by hand.
Something like this would work, as get_object_vars()
is called from inside the class, having access to private/protected variables:
function getJsonData(){
$var = get_object_vars($this);
foreach ($var as &$value) {
if (is_object($value) && method_exists($value,'getJsonData')) {
$value = $value->getJsonData();
}
}
return $var;
}
Object cloning is the act of making a copy of an object. As Cody pointed out, cloning in PHP is done by making a shallow copy of the object. This means that internal objects of the cloned object will not be cloned, unless you explicitly instruct the object to clone these internal objects too, by defining the magic method
__clone()
.If you don't utilize the
__clone
method, the internal objects of the new object will be references to the same objecs in memory as the internal objects of the original object that was cloned.Consider these examples:
Use cases for cloning would for instance be a case where you don't want outside objects to mess with the internal state of an object.
Let's say you have a class User with a internal object Address.
For arguments sake, let's say you don't want outside objects to mess with the internal Address of User objects, but you do want to be able to give them a copy of the Address object. The above example illustrates this. The
getAddress
method returns a clone of the address object to calling objects. This means that if the calling object alters the Address object, the internal Address of User will not change. If you didn't give a clone, then the outside object would be able to alter the internal Address of User, because a reference is given by default, not a clone.Hope this all makes some sense.
PS.:
Be aware though, that if Address would also have internal objects, you would have to make sure Address makes a deep copy of itself on cloning (as per my second example of this post) by defining
__clone()
in Address. Otherwise you will get headaches of trying to figure out why your data is screwed.