php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26364 initializing class in other class vars value problem
Submitted: 2003-11-23 10:02 UTC Modified: 2003-11-24 05:08 UTC
From: brightone at o2 dot pl Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.3.4 OS: windows xp
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: brightone at o2 dot pl
New email:
PHP Version: OS:

 

 [2003-11-23 10:02 UTC] brightone at o2 dot pl
Description:
------------
You have two classes, one initialized in another's contructor. After initializing the "child" class in "mother" constructor, all new values assigned to variables in mother are not visible in pointed classes.

Reproduce code:
---------------
<? class a{
  var $temp;
  var $pointer;
  function a(){
    $this->temp=10;
    $this->pointer=new b(&$this);
    $this->temp=20;
    echo " A: ".$this->temp;
  }
}
class b{
  var $root;
  function b($root){
    $this->root=$root;
  }
  function show_val(){ 
    echo " B: ".$this->root->temp;
}
}
$r=new a;
$r->pointer->show_val();  ?>

Expected result:
----------------
A: 20 B: 20

Actual result:
--------------
A: 20 B: 10

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-23 21:26 UTC] sniper@php.net
Change this:
 $this->root=$root;
To:
 $this->root =& $root;

and it'll work.

 [2003-11-24 05:02 UTC] brightone at o2 dot pl
Okay, you're right here, my mistake. But there is a bug anyway, I'll show it on little bigger example:

<? class a{
  var $temp;
  var $p_a;
  var $p_b;
  function a(){
    $this->p_b=new b(&$this);
    $this->p_c=new c(&$this);
  }

  function do_all(){
    $this->p_c->give_val();
    $this->p_b->show_val_c();
    echo " a: ".$this->p_c->something;
  }
}

class b{
  var $root;
  function b($root){
    $this->root=&$root;
  }
  function show_val_c(){
    echo " b: ".$this->root->p_c->something;
}
}

class c{
  var $root;
  var $something;
  function b($root){
    $this->root=&$root;
  }
  function give_val(){
    $this->something="something";
}
}

$r=new a;
$r->do_all();  ?>

Obviously, we want to see here "b: something b: something", but all we get is "b: a: something"
 [2003-11-24 05:08 UTC] brightone at o2 dot pl
There is a little mistake in a the code above, here it goes:

<? class a{
  var $temp;
  var $p_b;
  var $p_c;
  function a(){
    $this->p_b=new b(&$this);
    $this->p_c=new c(&$this);
  }

  function do_all(){
    $this->p_c->give_val();
    $this->p_b->show_val_c();
    echo " a: ".$this->p_c->something;
  }
}

class b{
  var $root;
  function b($root){
    $this->root=&$root;
  }
  function show_val_c(){
    echo " b: ".$this->root->p_c->something;
}
}

class c{
  var $root;
  var $something;
  function b($root){
    $this->root=&$root;
  }
  function give_val(){
    $this->something="something";
}
}

$r=new a;
$r->do_all();  ?>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 16:01:29 2024 UTC