Re: [PHP] Object References Problem

From: Michael Sims (mhsims_at_midsouth.rr.com)
Date: 10/31/03

  • Next message: Mike Migurski: "Re: [PHP] Object References Problem"
    To: php-general@lists.php.net
    Date: Thu, 30 Oct 2003 18:59:56 -0600
    
    

    On Thu, 30 Oct 2003 22:54:49 +0100, you wrote:

    >Hi there,
    >
    >I'm having trouble with passing objects as references. What I want to
    >do is something like this:
    [snip code]
    >The above code give the output:
    >
    >instance:
    >parent: test
    >
    >where I want it to give:
    >
    >instance: test
    >parent: test

    I don't pretend to fully understand PHP references, or the strange and
    mysterious ways that they work in regards to PHP objects, but I can
    tell you how to acheive the results you desire. Someone else will
    have to explain it. :)

    First of all, you need to use the "=&" operator inside the constructor
    for object_2. Secondly, you need to use the same "=&" operator when
    creating the new instance of object_1. Full code follows:

    class object_1
    {
        var $my_child;

        var $my_array;

        function object_1()
        {
            $this->my_array = array('id' => 0, 'name'=>'');
            $this->my_child = new object_2($this);
        }
      
    }

    class object_2
    {
        var $my_parent;
        function object_2(&$parent_object)
        {
            $this->my_parent =& $parent_object;
        }

        function set_parent()
        {
            $this->my_parent->my_array['id'] = 1;
            $this->my_parent->my_array['name'] = 'test';
        }
    }

    $instance =& new object_1();
    $instance->my_child->set_parent();

    echo "instance: ".$instance->my_array['name']."<br>";
    echo "parent:
    ".$instance->my_child->my_parent->my_array['name']."<br>";

    This outputs (on PHP 4.3.3):

    instance: test
    parent: test

    Alternatively, you can forgo the "=&" operator when creating a new
    instance of object_1 if you move the creation of the object_2 child
    out of object_1's constructor and into another method. Say we add
    this method to object_1:

        function create_my_child() {
          $this->my_child = new object_2($this);
        }

    Then you can do this:

    $instance = new object_1();
    $instance->create_my_child();
    $instance->my_child->set_parent();

    echo "instance: ".$instance->my_array['name']."<br>";
    echo "parent:
    ".$instance->my_child->my_parent->my_array['name']."<br>";

    This also outputs:

    instance: test
    parent: test

    I really can't explain why either of these approaches work, but I
    suspect that it has something to do with the phenomenon described
    here:

    http://www.php.net/manual/en/language.oop.newref.php

    The headache I currently have prevents me from fully understanding
    this, hopefully it will be clearer to you. :)

    If someone sees that I'm leading Gareth astray here, feel free to jump
    in and correct me...


  • Next message: Mike Migurski: "Re: [PHP] Object References Problem"

    Relevant Pages

    • Object References Problem
      ... I'm having trouble with passing objects as references. ... parent: test ...
      (php.general)
    • finding class parent information
      ... I can get all of the compponets in a class, and get references to ... JLabel called myLabel belongs to parent JPanel called myPanel ... JButton called myButton belongs to parent JPanel called myPanel ...
      (comp.lang.java.programmer)
    • clutching on to my final attributes...
      ... When I deserialize my Parent object my 2 references to my 'A' object will ... private final Child c; ... void update{ ...
      (comp.lang.java.programmer)
    • Re: Duplicate server entries for the same site code
      ... I think so - if the child site is no longer referenced at the parent. ... >> run PREINST /DELSITE to clean up references to the site if any>> ... >>> Neither log on the parent server reports any entries for either server. ...
      (microsoft.public.sms.setup)
    • Re: TMJ re-revisited
      ... (Newsgroup threading is done based on the References: and Message-ID: ... change off as a new thread, but with a link to the parent post in the ...
      (rec.games.roguelike.angband)