note 34543 added to language.references
| From: | gooseFX at rack1 dot php dot net | Date: | Wed, 30 Jul 2003 12:46:28 +0000 |
| Subject: | note 34543 added to language.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-53004@lists.php.net to get a copy of this message | ||
References under PHP is a real silly thing and i thought I had understood it but after call-time
pass-by-reference has been deprecated I have several problems with them.
For example I have a list object which stores ( as it says ) a list of objects or value types. Now I
have a method add($var) which adds the value to the list. Before call-time pass-by-reference has
been deprecated i just called the method like this:
$myList->add( &$myVar );
This version of my list could handle object and value types. But now this feature has gone and I
have to change my method argument to reference add(&$var), but this method does not take a value
type like add(17), because only variables can be passed by reference. A big problem for my complete
php library. What now???
My solution is a reference object which can be past by value but its internal reference will never
get lost.
class gfRef
{
var $ref = null;
function gfRef(&$ref)
{
$this->ref =& $ref;
$class_vars = get_class_vars(get_class($ref));
// create a reference to each variable
foreach( $class_vars as $class_var => $class_value )
{
$this->$class_var =& $ref->$class_var;
}
// copy all methods to the reference object
aggregate_methods ( $this, get_class($ref));
}
};
the reference object behave like the original object but each class variable is hold by reference.
You can pass the ref object by value but it points to the original object and has the same methods
and variables as the original one.
This is just a suggestion but I think this is the best way to skip around php's object-by-value
principle.
Have any suggestion then mail to: marcus@gooseflesh.de
----
Manual Page -- http://www.php.net/manual/en/language.references.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34543
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34543&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34543&report=yes