purencool Posted January 25, 2011 Share Posted January 25, 2011 hi phpfreaks I have an class that is called "connDatabase" what I want to do with it is either extend it or implement it. But I don't know which one to use. The other issue I have is say class "a" has three vars that the that the construct needs to obstantiate and "connDatabase" needs three of its own. How do I obstantiate? thanks for any help Quote Link to comment Share on other sites More sharing options...
trq Posted January 25, 2011 Share Posted January 25, 2011 You cannot implement a class, only an interface, so that solves that. I assume when you say obstantiate, you mean instantiate? You can setup a __construct in a child class that calls the parent _-construct() before doing its own work. class Foo extends Bar { public function __construct() { parent::__construct(); // do own setup } } On a side note. be careful extending a database classes. does the child really extend the database or just depend on it? There is a difference. Quote Link to comment Share on other sites More sharing options...
Zurev Posted January 25, 2011 Share Posted January 25, 2011 Extending is what you would want to do.... I believe you mean instantiate? Do you think you could post some code here? At least the classes and their constructs? Quote Link to comment Share on other sites More sharing options...
purencool Posted January 25, 2011 Author Share Posted January 25, 2011 sorry instantiate. Below are the two classes I want to join. I have code that needs to call on information out of the database. But what I am trying to achieve is to extend the connDatabase onto the Controller i will be able to query the database and not have to keep writing database connections. I have a number of classes that needs this sort of functionality. Note: if this is back practice please tell I would rather corrected now. class connDatabase { private $dbConnection private $unlock; private $path; protected $host; protected $database; protected $username; protected $password; public function __construct ($dbConnection,$unlock,$path){ $this->dbConnection = $dbConnection; $this->unlock = $unlock; $this->path = $path; $this->decryptLock(); } private function decryptLock(){ //decrypt //addhost and data //place into private } private function connection($query){ $db = new PDO("mysql:host=$this->host;dbname=$this->database", "$this->username", "$this->password" ); $db->beginTransaction(); $db->exec ($query); $db->commit(); $db->Null; } public function getConnection ($query){ $this->connection($query); } } class controller { private $pages; private $language; private $accessLevel; private $path; private $defaultpage; public function __construct($default,$path, $pages, $language, $accessLevel) { $this->pages = $pages;//array showing pages $this->language = $language; $this->accessLevel = $accessLevel; $this->path = $path; $this->defaultpage =$default; } private function findTech($request) { return $return; } public function findTechReturn($request) { return $return; } Quote Link to comment Share on other sites More sharing options...
trq Posted January 25, 2011 Share Posted January 25, 2011 I can tell just by the name 'controller' that it is not at all related to and therefore should not extend the database class. The database class might be a dependency, but that's it. Even then, any database interactions should likely be moved into models (but I won't go into that). You controller simply requires the Database class. This should be passed into your __construct() method. Quote Link to comment Share on other sites More sharing options...
purencool Posted January 25, 2011 Author Share Posted January 25, 2011 do you mean I can do something like this $controller = new controller(controller vars ,$database = new connDatabase($var, $var, $var)) add the database connection as a var inside the class class controller { private $pages; private $language; private $accessLevel; private $path; private $defaultpage; private $connectionDatabase; public function __construct($default,$path, $pages, $language, $accessLevel, $connectionDatabase) { $this->pages = $pages;//array showing pages $this->language = $language; $this->accessLevel = $accessLevel; $this->path = $path; $this->defaultpage =$default; $this->connection =$connectionDatabase; } Quote Link to comment Share on other sites More sharing options...
jcbones Posted January 25, 2011 Share Posted January 25, 2011 Here's some good reading for you. Quote Link to comment Share on other sites More sharing options...
btherl Posted January 25, 2011 Share Posted January 25, 2011 Yes you can do that. We have one app that does it that way, and another app that has the db connection in a global variable (not inside any class). Both of those approaches work. Quote Link to comment Share on other sites More sharing options...
purencool Posted January 25, 2011 Author Share Posted January 25, 2011 thanks for the help and the information. Quote Link to comment 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.