How to Create a Copy of an Object in PHP

How do I create a copy of an object in PHP?

In PHP 5+ objects are passed by reference. In PHP 4 they are passed by value (that's why it had runtime pass by reference, which became deprecated).

You can use the 'clone' operator in PHP5 to copy objects:

$objectB = clone $objectA;

Also, it's just objects that are passed by reference, not everything as you've said in your question...

How to push a copy of an object into array in PHP

Objects are always passed by reference in php 5 or later. If you want a copy, you can use the clone operator

$obj = new MyClass;
$arr[] = clone $obj;

How to make a copy of an object without reference?

<?php
$x = clone($obj);

So it should read like this:

<?php
function refObj($object){
foreach($object as &$o){
$o = 'this will change to ' . $o;
}

return $object;
}

$obj = new StdClass;
$obj->x = 'x';
$obj->y = 'y';

$x = clone($obj);

print_r($x)

refObj($obj); // $obj is passed by reference

print_r($x)

PHP deep clone object

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 {
function __clone() {
$this->template = new Template();
}
}

.

unserialize(serialize($object)); // would be another solution...

Deep-copy Object with Array of Object Properties - how to or alternative?

You could extend the approach from this question, and implement a custom __clone method for each of your classes. Since there don't seem to be any relations between the nodes or edges themselves, it should be enough to accomplish what you want.

It might be worth mentioning that, as described in the documentation, __clone is invoked on the new object just after cloning. It isn't actually responsible for cloning the object which might seem logical at first.

So, simplified for the Tree and Node class:

class Tree
{
private $nodes;

private $edges;

public function __clone()
{
$this->nodes = array_map(function ($node) {
return clone $node;
}, $this->nodes);

$this->edges = array_map(function ($edge) {
return clone $edge;
}, $this->edges);

// clone other properties as necessary
}
}

class Node
{
private $values;

private $someArray;

public function __clone()
{
$this->values = array_map(function ($value) {
return clone $value;
}, $this->values);

$this->someArray = array_map(function ($elem) {
return clone $elem;
}, $this->someArray);

// clone other properties as necessary
}
}

Just follow this pattern for each class down the road and it will recursively deep clone your whole tree.

How can I copy an object of another class?

try this, Once you instantiate a object, you can't change the class (or other implementation details)

You can simulate it like so:

<?php

class Test
{
private $id;
private $name;
private $age;
private $number;

public function getId() {
return $this->id;
}

public function setId($id) {
$this->id = $id;
return $this;
}

public function getName() {
return $this->name;
}

public function setName($name) {
$this->name = $name;
return $this;
}

public function getAge() {
return $this->age;
}

public function setAge($age) {
$this->age = $age;
return $this;
}

public function getNumber() {
return $this->number;
}

public function setNumber($number) {
$this->number = $number;
return $this;
}

public function toArray()
{
return get_object_vars($this);
}

}

class TestCopy extends Test
{
public $fakeAttribute;
}

function getTestCopy($object)
{
$copy = new TestCopy();

foreach($object->toArray() as $key => $value) {
if(method_exists($copy, 'set'.ucfirst($key))) {
$copy->{'set'.ucfirst($key)}($value);
}
}

return $copy;
}

$object = new Test();
$object->setId(1);
$object->setName('Tom');
$object->setAge(20);
$object->setNumber(10);

$copy = getTestCopy($object);
$copy->fakeAttribute = 'fake value';

echo "<pre>";
print_r($object->toArray());
print_r($copy->toArray());

output :

Array
(
[id] => 1
[name] => Tom
[age] => 20
[number] => 10
)
Array
(
[fakeAttribute] => fake value
[id] => 1
[name] => Tom
[age] => 20
[number] => 10
)

How do you copy a PHP object into a different object type

You could have your classes instantiated empty and then loaded by any number of methods. One of these methods could accept an instance of the parent class as an argument, and then copy its data from there

class childClass extends parentClass
{
function childClass()
{
//do nothing
}

function loadFromParentObj( $parentObj )
{
$this->a = $parentObj->a;
$this->b = $parentObj->b;
$this->c = $parentObj->c;
}
};

$myParent = new parentClass();
$myChild = new childClass();
$myChild->loadFromParentObj( $myParent );

Copy a Doctrine object with all relations

I never could get the deep copy function to operate correctly.

I manually coded a deep copy function for one of my models like this

public function copyAndSave ()
{
$filters = array('id', 'created');

$survey = $this->copy();

$survey->Survey_Entries = new Doctrine_Collection("Survey_Model_Entry");
$survey->Assignment_Assignments = new Doctrine_Collection("Assignment_Model_Assignment");
$survey->Survey_Questions = new Doctrine_Collection("Survey_Model_Question");

$survey->save();

foreach ($this->Survey_Questions as $question)
{
$answers = $question->Survey_Answers;
$newQuestion = $question->copy();
$newQuestion->survey_surveys_id = $survey->id;
$newQuestion->save();
$newAnswers = new Doctrine_Collection("Survey_Model_Answer");

foreach($answers as $answer)
{
$answer = $answer->copy();
$answer->save();
$answer->survey_questions_id = $newQuestion->id;
$newAnswers->add($answer);
}
$newQuestion->Survey_Answers = $newAnswers;

$survey->Survey_Questions->add($newQuestion);
}
return $survey->save();
}


Related Topics



Leave a reply



Submit