#38011 [NEW]: suggest $foo = &new Bar(); for PHP4
| From: | joliver at gmx dot at | Date: | Wed, 05 Jul 2006 01:18:54 +0000 |
| Subject: | #38011 [NEW]: suggest $foo = &new Bar(); for PHP4 | ||
| Groups: | php.doc | ||
| Request: | Send a blank email to phpdoc+get-969373451@lists.php.net to get a copy of this message | ||
From: joliver at gmx dot at
Operating system: all
PHP version: Irrelevant
PHP Bug Type: Documentation problem
Bug description: suggest $foo = &new Bar(); for PHP4
Description:
------------
Please provide an advice to PHP4 users to use
$foo = &new Bar();
instead of $foo = new Bar();
Contrary to the examples in the documentation, in PHP4
instances of objects should be initialisised with
$foo = &new Bar();
because here $foo is a reference to the created instance
of Bar, while for
$foo = new Bar();
$foo is a copy(!) of the instance.
Appart from performance and memory issues, in the second
case parameters in the constructors will be copied to,
because the whole object is copied. That means for
example, that for:
$foo = new Bar($this)
$foo is an instance of Bar where $this is not the owner of
that instance any more, because $foo is a copy of the
newly created instance.
Good example in german:
http://www.usegroup.de/software/phptutorial/klassen_und_objekte.html#ref_owner
Reproduce code:
---------------
<?php
class myC1
{
var
$greeting;
var $theC2;
function sayHello()
{
echo $this->greeting;
}
function myC1()
{
$this->greeting="Howdy";
$this->theC2=new myC2($this);
}
}
class myC2
{
var $owner;
function myC2(&$owner)
{
$this->owner=&$owner;
$this->owner->greeting="bah";
}
function something()
{
$this->owner->greeting="hulla";
// This is supposed to change myC1's greeting to hulla, overwriting
"bah".
}
}
$test=new myC1(); //$test=&new myC1();
$test->theC2->something();
$test->sayHello();
?>
Expected result:
----------------
$test=&new myC1();
returns the excepted result in PHP4. Please provide that
advice for PHP4 users in the documentation.
Actual result:
--------------
As described above, $test=new myC1(); returns a copy of
the instance, therefore $this in myC1 gets lost.
An advice for PHP4 users would be helpful, in my opinion
in PHP4
$foo=&new Bar(); should be used instead of
$foo=new Bar(;), which is not mentioned anywhere in the
documentation.
(of course i know in PHP5 this as been fixed because
objects are passed by reference.)
Thank you!
Oliver
--
Edit bug report at http://bugs.php.net/?id=38011&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=38011&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=38011&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=38011&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=38011&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=38011&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=38011&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=38011&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=38011&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=38011&r=support
Expected behavior: http://bugs.php.net/fix.php?id=38011&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=38011&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=38011&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=38011&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=38011&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=38011&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=38011&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=38011&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=38011&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=38011&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=38011&r=mysqlcfg