note 50016 deleted from language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:39:45 +0000 |
| Subject: | note 50016 deleted from language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155570@lists.php.net to get a copy of this message | ||
Note Submitter: S dot Radovanovic at TriMM dot nl
----
Sadly I have found out that I have been wrong about my statements below, why?
Well:
1. only the variables that were set in the constructor were 'live' in my referenced object
2. I was assigning this an object and not a reference
So:
I fixed nr. 1 by adding the & when initializing the object (this way this works on the
initialized object and not a copy of it)
<?php
//screen factory
$objErrorConfig = & new Config("error.conf");
$objErrorConfig->setSection("messages");
//object factory
//If no file is stated, the last one is used, this way this instance will have the reference to the
previously created instance of objErrorConfig in object screen
$objErrorConfig = & new Config();
$errorMessage = $objErrorConfig->get($errorName);
?>
Now the variables assigned after the constructor
($objErrorConfig->setSection("messages");) will also be 'live' in the static
obj array.
I had to find a workaround for nr.2, since it is impossible to assign a reference to this.
That's why I used code proposed by others, nl. I referenced all the members of the objects:
<?php
//Because we cannot make a direct reference from our object (by doing $this = & $theObject)
//we'll make references of our members
$arrClassVars = get_class_vars(get_class($theObject));
foreach($arrClassVars as $member=>$value) {
$this->$member = &$theObject->$member;
}
//To make sure we are working with a reference we will store our new object as the reference
//in the singeltonobject array (so all other initialized (referenced) objects will have the
//newest one as super referer
$arrSingletonObject[$this->_configfile] = & $this;
?>
So in the end, I had better used what everbody was using (creating a Singleton through an method,
instead of through the constructor), but hey, I learned something again :)