Thirupathi
Joined: 01 Dec 2009 Posts: 70
|
Posted: Sat Feb 06, 2010 6:51 am Post subject: Need to develop cloning concept in php5 |
|
|
Hi friends,I have posted topic regarding how the cloning concept was developed in php5.What is the need of clone object.
This simple example will give you aware of cloning concept.
<?php
class Person {
var $name;
function getName() {
return $this->name;
}
function setName($name) {
$this->name = $name;
}
function Person($name) {
$this->setName($name);
}
}
function changeName($person, $name) {
$person->setName($name);
}
$person = new Person("Raj");
changeName($person, "Ratan");
print $person->getName();
?>
In PHP 4, this piece of code would print out "Raj". The reason is that we pass the object $person to the changeName() function by-value, and thus, $person is copied and changeName() works on a copy of $person.
In PHP 5, the infrastructure of the object model was rewritten to work with object handles. Unless you explicitly clone an object by using the clone keyword you will never create behind the scene duplicates of your objects. In PHP 5, there is neither a need to pass objects by reference nor assigning them by reference.
I think it might be help you.
Thanks and Regards,
Thirupathi.J |
|