Re: OO parent/child relationship

From: Date: Mon, 06 Oct 2003 03:14:47 +0000
Subject: Re: OO parent/child relationship
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-165124@lists.php.net to get a copy of this message
Tom Rogers wrote:
To do this I have my classes register themselves in a global array. For example a mysql class which gets used a lot does this in its constructor: function mysql_class($db='',$ini=''){ global $class_ref; $class_ref["mysql_class"] =& $this; . . . } Then any class that needs access to mysql_class does this in the constructor: class galleryClass( var $mysql; var $db = 'test'; function galleryClass(){ global $class_ref; if(!empty($class_ref['mysql_class'])):
     $this->mysql =& $class_ref['mysql_class'];
else:
     $this->mysql = new mysql_class();
endif; if(!empty($this->mysql)):
     $this->con = $this->mysql->get_handle($this->db);
endif; . . } . . } This way there is only need for 1 instance of the mysql class. I use global as a bad habit, but you can do the same with $GLOBALS['class_ref'] :)
I asked about something similar to your suggetion the other day -> http://marc.theaimsgroup.com/?l=php-general&m=106519101018470&w=2

« previous php.general (#165124) next »