note 50041 added to language.types.object
| From: | mortoray at ecircle-ag dot com | Date: | Wed, 16 Feb 2005 10:07:40 +0000 |
| Subject: | note 50041 added to language.types.object | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85106@lists.php.net to get a copy of this message | ||
If you use new to create items in an array, you may not get the results you want since the
parameters to array will be copies of the original and not references.
By Example:
class Store {
var $item = 3;
}
$a = array( new Store() );
$b = $a;
$a[0]->item = 2;
print( "|" . $b[0]->item . "| <br>" ); //shows 3
$a = array();
$a[] =& new Store();
$b = $a;
$a[0]->item = 2;
print( "|" . $b[0]->item . "| <br>" ); //shows 2
This is extremely important if you intend on passing arrays of classes to functions and expect them
to always use the same object instance!
Note: The following syntax is desired (or maybe even the default notation should translate as this):
$a = array( &new Store() );
----
Manual Page -- http://www.php.net/manual/en/language.types.object.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50041
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50041&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50041&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50041&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50041&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50041&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50041&report=yes
Search -- http://master.php.net/manage/user-notes.php