1

I Have a ListNode built that has the following structure:

 class MyNode {
    private  $weight;
    private  $children;
    private  $t1;
    private  $t2;
    private  $t3;
    *** 
    more variables
    ***
    function __constructr($weight, $t1, $t2, $t3, $children = array()) {
        $this->weight = $weight;
        $this->children = $children;
        $this->t1 = $t1;
        $this->t2 = $t2;
        $this->t3 = $t3;
    }

Now I create 5 Nodes that have same data but different weight.

    $n1 = new MyNode(25, 't_1', 't_2', 't_3');
    $n2 = new MyNode(30, 't_1', 't_2', 't_3');
    $n3 = new MyNode(49, 't_1', 't_2', 't_3');
    $n4 = new MyNode(16, 't_1', 't_2', 't_3');
    $n5 = new MyNode(62, 't_1', 't_2', 't_3');

Note that the t1, t2 and t3 can be different but for this 5 nodes they are same. Instead of doing above i want to do the following using some kind of clone function

    $n1 = new MyNode(25, 't_1', 't_2', 't_3');
    $n2 = $n1->clone(array('weight' => 30));
    $n3 = $n2->clone(array('weight' => 49));
    $n4 = $n4->clone(array('weight' => 16));
    $n5 = $n5->clone(array('weight' => 62));

clone function takes array of keys being the variable names inside MyNode that i want to change and their values. so array('weight' => 30) should change $this->weight = 30; Im stuck accessing the variable from array. It should create a new node with all the values being same as its current node but modify only the ones that are in array.

   function clone($changeVariables) {
      -----
   }

3 Answers 3

1

Try this:

$obj = clone $this;

foreach ($changeVariables as $field => $val) {
    $obj->{$field} = $val;
}
return $obj;
1

Observation

  • You can not Implement a method or function called clone its a reserved word
  • That is not how to clone object in php
  • __constructr is wrong and not valid way to set construct in php

Here is what you need :

class MyNode {
    private $weight;
    private $children;
    private $t1;
    private $t2;
    private $t3;

    function __construct($weight, $t1, $t2, $t3, $children = array()) {
        $this->weight = $weight;
        $this->children = $children;
        $this->t1 = $t1;
        $this->t2 = $t2;
        $this->t3 = $t3;
    }

    public function getClone(array $arg) {
        $t = clone $this;
        foreach ( $arg as $k => $v ) {
            $t->{$k} = $v;
        }
        return $t;
    }
}

$n1 = new MyNode(25, 't_1', 't_2', 't_3');
$n2 = $n1->getClone(array(
        'weight' => 30
));

print_r($n1);
print_r($n2);

Output

MyNode Object
(
    [weight:MyNode:private] => 25
    [children:MyNode:private] => Array
        (
        )

    [t1:MyNode:private] => t_1
    [t2:MyNode:private] => t_2
    [t3:MyNode:private] => t_3
)
MyNode Object
(
    [weight:MyNode:private] => 30
    [children:MyNode:private] => Array
        (
        )

    [t1:MyNode:private] => t_1
    [t2:MyNode:private] => t_2
    [t3:MyNode:private] => t_3
)
0

Variable variables is one solution:

foreach ($changeVariables as $key => $value) {
    $this->{$key} = $value;
}

You could enhance it by checking to see if $this->{$key} exists before allowing it to be set.

http://php.net/manual/en/language.oop5.cloning.php

Overall result would be something like:

function clone($changeVariables) {
    $newObj = clone $this;
    foreach ($changeVariables as $key => $value) {
        $newObj->{$key} = $value;
    }
    return $newObj;
}
1
  • that changes the values of the current node but i want to create a clone of current node that has exactly same data as current node but only difference is whatever is in array.
    – GGio
    Commented Mar 21, 2013 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.