Should have asked someone this a long time ago.
What is the best way to use other classes within another class?
For instance, lets say I have an application class:
class Application { public function displayVar() { echo 'hello world'; } }
and a database class
class Database { // connects to db on construct public function query() { // queries db } }
now, i want to add a function to my application class that uses a function from the db class
class Application { public function displayVar() { echo 'hello world'; } public function getVar() { global $db; $sql = foo; $db->query($sql); } }
so then I have
$db = new Database(); $app = new Application(); $app->getVar('var');
Is there a better way of doing this? Really what I am looking for is the standard way of doing it, not another way of rigging it.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
There are a couple of ways of doing that. Global variables is certainly one way and the most looked down upon too. You can create a Singleton and all other classes that need database access would call upon this singleton.
final class Database { private static $connection; public static function getInstance() { if(self::$connection == NULL) { self::$connection = // init your database connection } return self::$connection; } }
And use this database connection object in whatever class needs it.
class Application { public function displayVar() { echo 'hello world'; } public function getVar() { $db = Database::getInstance(); $sql = foo; $db->query($sql); } }
This is all well for a start and a great step beyond using global variables, but you can do better with Dependency Injection. Dependency Injection is a simple concept that if a class has any external dependencies, such as the database connection in your example, you explicitly pass those to the needy class in its constructor or a method. So the new code would look something like Jonathan’s solution. A major advantage of using dependency injection is in unit testing, where you can easily replace this actual database object with a mock object and pass it to whoever needs it.
class Application { private $db; public function __construct(Database $db) { $this->db = $db; } public function displayVar() { echo 'hello world'; } public function getVar() { $sql = foo; $this->db->query($sql); } }
For smaller projects, you can easily do it yourself. For large projects, there are various DI frameworks available for PHP
Method 2
$db
could be a property of your Application
class. Any reference to it from within an instance of Application
would be done via $this
– $this->db
class Application { private $db = null; public function setDB($name) { $this->db = new Database($name); } }
Method 3
Include the class file (or set up autoinclude) in each PHP file that needs the class in question. Then instantiate it as needed.
If you need to have a “common” instance of an object, you can look at the Singleton and Factory patterns:
Singleton Pattern
Factory Pattern
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0