2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
4 // Copyright (c) 2003 Laurent Bedubourg
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
24 * Create a new Ref object referencing specified object or variable.
26 * Any modification of the source object modify the Refence content, any
27 * modification of the reference obj modify the source object.
29 * The referenced object can be reached using the reference object using the
37 * $ref_a->obj->doBar(); // call doBar() on $a
41 * @param mixed $obj -- the object or variable to reference.
50 * Returns true if the object is a Ref object.
52 * @param mixed $obj -- any variable that must be tested.
58 return is_object($obj) && get_class($obj) == "ref";
65 * There's a lot to say about references in PHP. The main problem come from the
66 * fact that until ZendEngine2, objects are copied if not referenced.
68 * This class helps keeping references to objects even while modifying arrays.
72 * If an array stores objects, using array_values will copy its
75 * If an array stores Ref objects referencing real objects, array_values copy
76 * Ref objects but these copies still reference the source object.
78 * Until ZendEngine2, it is safer to use Ref objects in stead of object
83 * $a = new Foo('bar');
91 * This object will have no meaning in php 4.4 as PHP will then use references
92 * everywhere objects are involved.
94 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
99 * php reference to data.
104 * Reference constructor.
108 * If $r is a reference, the new reference will just be a copy of the
111 * A Ref cannot reference a Ref.
113 * @param mixed $r -- the object or data to reference.
118 $this->obj
=& $r->obj
;
125 * Retrieve the type or the referenced variable.
131 return gettype($this->obj
);
135 * Return the class name of the referenced variable (if object).
137 * @return string (empty if not an object)
139 function getClassName()
141 return get_class($this->obj
);