[php] pass by reference

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
In loop elke keer tegen een probleem op dat te maken heeft met "pass by reference".

Hieronder heb ik het probleem in een simpele structuur geschreven als voorbeeld.

* Ik heb een root node
* De root node heeft 2 kinderen
* Ik vraag het eerste kind op
* Vanuit het opgevraagde kind vraag ik zijn parent op (dat moet weer een referentie naar de root node geven)
* Ik wijzig de opgevraagde parents' naam
* Ik print de root node, maar de wijziging in naam is niet doorgevoerd

Tips?

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Node
{
    var $name;
    var $parent;
    var $children;

    function Node($name,$parent)
    {
        $this->name = $name;
        $this->parent = &$parent;
        $this->children = array();
    }

    function addChild($name)
    {
        $child = new Node($name,&$this);
        array_push($this->children,&$child);
    }

    function getChild($i)
    {
        return $this->children[$i];
    }

    function getParent()
    {
        return $this->parent;
    }

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

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

class Test
{
    function Test()
    {
        $root = new Node("kees");
        $root->addChild("marietje");
        $root->addChild("klaasje");

        $testChild = $root->getChild(0);
        $parent = $testChild->getParent();
        $parent->setName("kees2");

        echo $root->getName();
    }
}

new Test();

Acties:
  • 0 Henk 'm!

  • wim__k
  • Registratie: Februari 2003
  • Laatst online: 21-11-2020
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?
class Node
{
    var $name;
    var $parent;
    var $children;

    function Node($name)
    {
        $this->name = $name;
        $this->children = array();
    }

    function addChild($name)
    {
        $child = new Node($name);
        $child->setParent(&$this);
          $this->children[] =& $child;
    }

    function &getChild($i)
    {
        return $this->children[$i];
    }

    function &getParent()
    {
        return $this->parent;
    }
    
    function setParent(&$parent)
    {
         $this->parent =& $parent;
    }

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

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

class Test
{
    function Test()
    {
        $root = new Node("kees", NULL);
        $root->addChild("marietje");
        $root->addChild("klaasje");

        $testChild =& $root->getChild(0);
        $parent =& $testChild->getParent();
        $parent->setName("kees2");

        echo $root->getName();
    }
}

new Test();
?> 


Toevalig heb ik laatst bijna precies hetzelfde geschreven ;) Toen kwam ik er nog eens achter dat het hele pass by reference/copy verhaal in php4 echt vreselijk is. Maar met veel &'s neerzetten lukt het wel ;)

Acties:
  • 0 Henk 'm!

Verwijderd

PHP:
1
        $root = new Node("kees");


veranderen in

PHP:
1
        $root = new Node("kees",$this);

en het werkt :)

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Thanks! na toevoegen van & voor de get'ers werkt het prima