note 50016 added to language.variables.scope

From: Date: Tue, 15 Feb 2005 15:50:01 +0000
Subject: note 50016 added to language.variables.scope
Groups: php.notes 
Request: Send a blank email to php-notes+get-85073@lists.php.net to get a copy of this message
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 :) ---- Manual Page -- http://www.php.net/manual/en/language.variables.scope.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50016 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50016&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50016&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50016&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50016&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50016&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50016&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85073) next »