Re: PHP5 OOP

From: Date: Tue, 10 Aug 2004 18:48:32 +0000
Subject: Re: PHP5 OOP
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-193357@lists.php.net to get a copy of this message
From: "Joel Kitching" <linolium@gmail.com> > Hello, I'm trying to get the hang of OOP here but can't quite figure > out how to relate these classes. I've got one for the main project, > one for the database, and one for a user. Somehow I need to get the > user access to the database, without cumbersome constructor calls > involving a copy of the instance of the class itself. > > // main project class > class gfusion { > protected static $db; > function __construct() { > $this->db = new db; > } > } > > > // database class > class db { > private $link; > private $query; > private $result; > ... > function query($query); > function fetch_row(); > function fetch_rows(); > ... > } > > > // user class > class user { > private $id; > private $group_id; > private $login; > private $password; > /* Somehow I need to get the db class instance here. */ > > function __construct($id = false) { > if (is_numeric($id)) { > print_r($this); > $this->db->query('SELECT * FROM user WHERE id = ' . $id); > $user_info = $this->db->get_row(); > > $this->id = $user_info['id']; > $this->group_id = $user_info['group_id']; > $this->login = $user_info['login']; > $this->password = $user_info['password']; > } > } > ... > } > > > I tried extending the user class from the project class, but that > didn't work, because the $db var was empty. I tried changing it to > static, but it didn't inherit the $db variable for some reason. So, > how can I make this work, so I can write a bunch of classes that can > blindly use $this->db or something similar without having to worry > about setting it in the constructor? I thought about setting it as a > global, but that didn't seem very... OOP. See here: http://www.php.net/manual/en/language.oop5.decon.php and note this: Note: Parent constructors are not called implicitly. In order to run a parent constructor, a call to parent::__construct() is required. ---John Holmes...

« previous php.general (#193357) next »