Re: oo design question - how to avoid a factory
| From: | Stefan Ohrmann | Date: | Tue, 21 Dec 2004 20:58:04 +0000 |
| Subject: | Re: oo design question - how to avoid a factory | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-35212@lists.php.net to get a copy of this message | ||
Hello,
Am 21.12.2004 18:57 schrieb Justin Patrin:
> I have seen other package which do this as well. The XML_Parser, for
> example, would parse everything fine but I wouldn't get anything from
> the class unless I used =& when I instantiated the object. This is a
> strange PHP4 bug.
No its a consequence of the default behavior of "=", thats also aplies
to objects in PHP 4. This will also used during object constructing
without &. Especially if you assign $this to another class. Try the
following example from <news:1072180.jR69Lj6CUh@malkusch.de>:
class A {
var $b;
function addB(& $b) {
$this->b = & $b;
}
function setB($b) {
$this->b->setB($b);
}
}
class B {
var $a;
var $b = 'B';
function B(& $a) {
$this->a = & $a;
$this->a->addB($this);
}
function getB() {
return $this->b;
}
function setB($b) {
$this->b = $b;
}
}
$a = & new A(); // Here is the & irrelevant
$b = & new B($a); // But now you need it
$a->setB('A');
echo $b->getB(); // prints 'A'
If you use $b = new B($a) then 'B' will be printed.
Some useful links about this topic:
<http://www.zend.com/zend/art/ref-count.php>
<http://www.php.net/manual/en/language.oop.newref.php>
Back to Structures_Datagrid:
In Structures_DataGrid_Renderer::setRenderer() which is called by the
constructor you assign $this to the renderer instance. So that the
renderer instance works with an other $this than you beacause "=" copied
the instance.
In PHP4 its strongly recommended to use "=&" for object construction,
all other leads to strange errors, as you could see.
regards
Stefan