Ivan Ivković Posted December 18, 2011 Share Posted December 18, 2011 It seems that a class can't inherit an object as attribute? I'm trying to make my UpperNavigation class access the $this -> db attribute which is a mysqli connection. Is this possible? If not, how do I manage my connection in other classes? class Connection{ public $state = false; public $db; public $db_table_names; function __construct($name){ include('dbconfig.php'); $this -> db = new mysqli($dbconfig[$name]['server'], $dbconfig[$name]['username'], $dbconfig[$name]['password'], $dbconfig[$name]['database']); $this -> state = true; $this -> db_table_names = $db_table_names; $this -> db -> query("SET NAMES 'UTF8'"); } function close(){ if($this -> state == true){ $this -> state = false; $this -> db -> close(); unset($this -> db); } } } This is my attempt of accessing Connection::$db connection. class UpperNavigation extends Connection{ public $all_parents_sorted; public function printUpperNavigation(){ echo '<a href=\''.$this -> current_page.'\'>'.$this -> title . '</a>'; } public function printSearchBar(){ echo '<input type=\'text\' value=\'Search by name...\' onfocus="if(this.value == \'Search by name...\'){this.value = \'\';}" onblur="if(this.value == \'\'){this.value = \'Search by name...\';}"/>'; } public function fetchAllParents($sp_id){ $latest_id = $sp_id; if(isset($this -> db)){ echo 'yes'; }else{ echo 'no'; } $query = 'SELECT default_name, type, sp_id FROM ' . $this -> db_table_names['sport'] . ' WHERE sp_id = "' . $sp_id . '"'; $result = $this -> db -> query($query); // THIS IS THE PROBLEM } } Quote Link to comment https://forums.phpfreaks.com/topic/253433-i-need-help-with-some-connection-in-oop-newbie/ Share on other sites More sharing options...
dzelenika Posted December 18, 2011 Share Posted December 18, 2011 Of course that class can inherit object as attribute. Might I see what error do You get. The best way to manage connection class is singelon. More abut singelton read here: http://php.net/manual/en/language.oop5.patterns.php Quote Link to comment https://forums.phpfreaks.com/topic/253433-i-need-help-with-some-connection-in-oop-newbie/#findComment-1299063 Share on other sites More sharing options...
gizmola Posted December 19, 2011 Share Posted December 19, 2011 Ivan, Your specific issue based on the code you provided, is that the connection class does not have a query method. So of course you're getting an error. This is a very good example of where dependency injection is (in my opinion) a superior solution. Rather than try and explain it I'll just point you to this http://components.symfony-project.org/dependency-injection/documentation. They do a fantastic job talking about why DI is a great approach, and also provide a DI Container class that is at the heart of the symfony 2 framework. You don't have to use it, but it's there in case you like what you see. The problem with your connection class as I see it, is that it is actually attempting to be a connection class, and a model class. In other words, you have a mixture of database connection activity and setup, with specific data and tables. I would recommend that you break those out into discrete classes. Have a model class for any specific table. You can pass an instance of your database connection object to the model class in the constructor. Likewise your UpperNavigation class should not be trying to inherit from connection, as it has nothing to do with connections. It is not a subtype of connection -- it s purpose is to facilitate the output of your menu from the looks of it. That class should not have the internals of a specific database query -- simply pass in either a specific model, or array of model objects that make sense. From the look of what you have there is only one table being used: db_table_names['sport']. Let's say that instead you had a SportModel.class.php class definition. So someone might expect to find code like this: $connection = new Connection($dbconfig); $sportModel = new SportModel($connection); $upperNav = new UpperNavigation($sportModel); Obviously this is just a sketch, but I wanted to give you an idea of how you might simplify and decouple your classes. Quote Link to comment https://forums.phpfreaks.com/topic/253433-i-need-help-with-some-connection-in-oop-newbie/#findComment-1299095 Share on other sites More sharing options...
Ivan Ivković Posted December 19, 2011 Author Share Posted December 19, 2011 dzelenika : Fatal error : Call to a member function query() on a non-object in sportvillecms\classes\page.php on line 152 gizmola: It's not my class that has the query() method, but the object $db; that I'm trying to pass by using extend feature. REMINDER: $result = $this -> db -> query($query); What You are saying would be: $result = $this -> query($query); // and that is not the case. Thanks you for answering! Hope you help me further in time. Quote Link to comment https://forums.phpfreaks.com/topic/253433-i-need-help-with-some-connection-in-oop-newbie/#findComment-1299290 Share on other sites More sharing options...
dzelenika Posted December 19, 2011 Share Posted December 19, 2011 Your db connection is never established. Constructor of parent class should be called explicitly. Try to log constructor execution by echoing some text Quote Link to comment https://forums.phpfreaks.com/topic/253433-i-need-help-with-some-connection-in-oop-newbie/#findComment-1299512 Share on other sites More sharing options...
Ivan Ivković Posted December 20, 2011 Author Share Posted December 20, 2011 It is (I suppose). I'll copy the objects in the content file, too when I get to my PC. Quote Link to comment https://forums.phpfreaks.com/topic/253433-i-need-help-with-some-connection-in-oop-newbie/#findComment-1299579 Share on other sites More sharing options...
gizmola Posted December 20, 2011 Share Posted December 20, 2011 dzelenika : Fatal error : Call to a member function query() on a non-object in sportvillecms\classes\page.php on line 152 gizmola: It's not my class that has the query() method, but the object $db; that I'm trying to pass by using extend feature. REMINDER: $result = $this -> db -> query($query); What You are saying would be: $result = $this -> query($query); // and that is not the case. Thanks you for answering! Hope you help me further in time. Ok, I see the issue, I missed the mysqli call. Yes, so the problem is that you have to explicity call the parent class constructor in your subclass: class UpperNavigation extends Connection { public function __construct() { parent::__construct(); } This issue is described clearly in the php manual here: http://us.php.net/manual/en/language.oop5.decon.php Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. Quote Link to comment https://forums.phpfreaks.com/topic/253433-i-need-help-with-some-connection-in-oop-newbie/#findComment-1299855 Share on other sites More sharing options...
Ivan Ivković Posted December 21, 2011 Author Share Posted December 21, 2011 Thank you! I'll try that. Quote Link to comment https://forums.phpfreaks.com/topic/253433-i-need-help-with-some-connection-in-oop-newbie/#findComment-1300024 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.