Millar Posted January 23, 2008 Share Posted January 23, 2008 Hello, I want class db (which extends class base) to be able to access class conf (which extends class base). When I print out the information of the base (parent class) class, I can access variables/functions defined in the base, db and conf classes. When I print the information of the db class (child of base) I can access variables/functions of only the base and db classes. So why can I not reference variables from my config class from within the sibling class? Thanks. Here is the print_r output: conf Object ( [db_host] => localhost [db_user] => * [db_pass] => * [db_database] => * [db_prefix] => * [db_type] => * [version] => 1.0 ) db Object ( [connection] => [version] => 1.0 ) base Object ( [version] => 1.0 [conf] => conf Object ( [db_host] => * [db_user] => * [db_pass] => * [db_database] => * [db_prefix] => * [db_type] => * [version] => 1.0 ) [db] => db Object ( [connection] => [version] => 1.0 ) ) Why does db Object not look like this?: db Object ( [connection] => [version] => 1.0 [conf] => conf Object ( [db_host] => * [db_user] => * [db_pass] => * [db_database] => * [db_prefix] => * [db_type] => * [version] => 1.0 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/87457-referencing-a-parent-class-other-children/ Share on other sites More sharing options...
trq Posted January 24, 2008 Share Posted January 24, 2008 So why can I not reference variables from my config class from within the sibling class? Because they are not related. What does base hold that needs to be shared between config and db? Quote Link to comment https://forums.phpfreaks.com/topic/87457-referencing-a-parent-class-other-children/#findComment-447703 Share on other sites More sharing options...
svivian Posted February 4, 2008 Share Posted February 4, 2008 I think I'm having a similar problem. I have a class, artist_control, which has an instance of another class, database, as a variable (for connecting to database, executing queries, etc). Also in my artist_control class, in one of the functions I have an instance of an artist class. My artist class needs to access my database class in order to run some SQL queries. How can I do this? I tried using global $db; (the variable was set in artist_control) but that didn't work. Is it a good idea to pass in the $db variable (perhaps by reference)? Quote Link to comment https://forums.phpfreaks.com/topic/87457-referencing-a-parent-class-other-children/#findComment-458054 Share on other sites More sharing options...
svivian Posted February 4, 2008 Share Posted February 4, 2008 Oh, I also can't have the $db as part of my artist class instead, because there will be other classes (eg album) that will need it too. Quote Link to comment https://forums.phpfreaks.com/topic/87457-referencing-a-parent-class-other-children/#findComment-458062 Share on other sites More sharing options...
svivian Posted February 6, 2008 Share Posted February 6, 2008 Hmm... well for now I've set $db as a global variable from the artist_control class, but there must be a better way of doing it, surely? Quote Link to comment https://forums.phpfreaks.com/topic/87457-referencing-a-parent-class-other-children/#findComment-460104 Share on other sites More sharing options...
BrandonK Posted February 6, 2008 Share Posted February 6, 2008 Why can't your artist class create its own instance of the db? Use a singleton pattern if you are afraid of multiple connections, but why should one class have to share with another? What if your artist_control class changes completely to a different database type? My thought would be to create layers, and don't make them dependent on each other. Quote Link to comment https://forums.phpfreaks.com/topic/87457-referencing-a-parent-class-other-children/#findComment-460114 Share on other sites More sharing options...
aschk Posted February 11, 2008 Share Posted February 11, 2008 If you think about it from a semantic point of view, is "artist_control" really a descendent of "database" ? It USES a database, but as far as I can make out it doesn't DESCEND (i.e isn't a child) of database. Why not have your artist_control simply use a database connection. e.g. class artist_control { private $dbconn; public function __construct(){ $this->dbconn = new Database(); } public function getArtists(){ $artists = $this->dbconn->query("SELECT * FROM arists"); return $artists; } } Even better (as brandon mentioned) have the Database class be a singleton, meaning you can use $this->dbconn = Database::getInstance(); and share this between any other classes that require db connectivity. This saves database connections (although php will merge them for you) and memory for allocation of new classes. Quote Link to comment https://forums.phpfreaks.com/topic/87457-referencing-a-parent-class-other-children/#findComment-463912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.