[PHP-DEV] PHP 4.0 Bug #9722: foreach() operates on a copy of an object

2001-03-13 Thread johanp

From: [EMAIL PROTECTED]
Operating system: Linux 2.2.16
PHP version:  4.0.4pl1
PHP Bug Type: Class/Object related
Bug description:  foreach() operates on a copy of an object

The following two array looping statements are not equal
(Assuming Update() updates some instance variables in the 
object)

foreach( $objs as $o )
   $o->Update()


and


for($i=0; $iUpdate();


In the first case the Update() method will change an anonymous copy of the object in 
the array while in the second case the update will work on the object in the array. 
Hence any changes that Update() might do will in the
first case not be done for the objects in the array.

Not sure if this is a genuine bug or just a consequence of the copying semantics for 
objects in PHP.




-- 
Edit Bug report at: http://bugs.php.net/?id=9722&edit=1



-- 
PHP Development Mailing List 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP-DEV] PHP 4.0 Bug #8935 Updated: A reference to 'this' can not be used in the constructor method for a class.

2001-03-13 Thread johanp

ID: 8935
User Update by: [EMAIL PROTECTED]
Status: Closed
Bug Type: Class/Object related
Description: A reference to 'this' can not be used in the constructor method for a 
class.

I haven't tried the $a = & new syntax 

However, is that really a working solution? This would
require all clients of that class to actually have to know that the constructor would 
like to use a &$this reference and hence the clients must know about the specific 
implementation.

Is that such a good idea? Wouldn't it be better to allow
the use of &$this in a constructor as default without having to create the class with 
this special version of new().

or did I mmiss something here?


Previous Comments:
---

[2001-03-09 18:24:38] [EMAIL PROTECTED]
I have added some notes to the documenation, as far as the
the site has been updated its documenatation check it out
"language.oop.newref.html" (http://snaps.php.net/manuals/)
it may take a day or two ...

---

[2001-03-09 17:26:53] [EMAIL PROTECTED]
not really this is only a duplicate report for an already
fixed bug...

---

[2001-03-09 17:09:08] [EMAIL PROTECTED]
It's fixed -> closed.

--Jani


---

[2001-03-09 13:50:45] [EMAIL PROTECTED]
this has been fixed for months,
please use =& new instead of = new in this special case

---

[2001-02-24 14:14:09] [EMAIL PROTECTED]
Please see the 4.0.4 release change log a lot of things along this line were fixed.

James

---

The remainder of the comments for this report are too long.  To view the rest of the 
comments, please view the bug report online.

Full Bug description available at: http://bugs.php.net/?id=8935


-- 
PHP Development Mailing List 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP-DEV] PHP 4.0 Bug #8935: A reference to 'this' can not be used in the constructor method for a class.

2001-01-26 Thread johanp

From: [EMAIL PROTECTED]
Operating system: Linux/WinNT
PHP version:  4.0.4
PHP Bug Type: Class/Object related
Bug description:  A reference to 'this' can not be used in the constructor method for 
a class.

The following script demonstrates the problem.

a=new A();
$this->a->Init();
echo "A val before calling change: ".$this->a->val."";
$this->a->bobj->ChangeA();
echo "A val after calling change (should be 3): ".$this->a->val."";
}
}

class A {
var $val=1;
var $bobj=null;

function A() {
   $this->bobj = new B();
}
function Init() {
   // Workaround it is safe to use a refernce to this outside the constuctor
   $this->bobj->Init(&$this);
}
function SetVal($v) {
$this->val=$v;
}
};

class B {
  var $aobj=null;
  function B() {
  }
  function Init(&$obj) {
 $this->aobj=&$obj;
  }
  function ChangeA() {
 $this->aobj->SetVal(3);
  }
};

class ContainerA1 {
var $a;
function ContainerA1() {
$this->a=new A1();
echo "A val before calling change: ".$this->a->val."";
$this->a->bobj->ChangeA();
echo "A val after calling change (should be 3): ".$this->a->val."";
}
}

// BUG
class A1 {
var $val=1;
var $bobj=null;

function A1() {
$this->bobj = new B();

// BUG. A reference of 'this' is NOT passed here as it seems but a copy!
// Hence the bobj will contain another copy of A1 and not the one we
// are just creating.
$this->bobj->Init(&$this);
}
function SetVal($v) {
$this->val=$v;
}
};

echo "Demonstration of 'this' bug.";
echo "Using class A (workaround)";
$c1=new ContainerA();

echo "Using class A1 (BUG)";
$c1=new ContainerA1();

?>


-- 
Edit Bug report at: http://bugs.php.net/?id=8935&edit=1



-- 
PHP Development Mailing List 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]