0

I have one database wrapper class. How should I use this class object to execute query in other class?

$liveresellerdb=new Database('host','user','spswd','db'); $fetch = $liveresellerdb->getResult($select_resellerData); 

How should I include the database object in my one class?

Class one { function a (){ $sql="select * from table "; //how should i execute here my query i mean to say //every time i can't create the new object // i want to know the good method by which i just execute the query by // giving the database name } } 
1
  • much better question then the last post ! Commented Mar 16, 2011 at 11:27

4 Answers 4

3

I'd advise against the Singleton pattern (using static members is a variation of this pattern) and use Dependency Injection instead. This means that you pass the database object to your service (the class that uses the connection) through the constructor.

Here is an example.

class UserFinder { private $db; public function __construct(Database $db) { $this->db = $db; } public function findAllActive() { $sql = 'SELECT * FROM users WHERE active = 1'; return $this->db->executeAndFetchAll($sql); } } $db = new Database($host, ...); $finder = new UserFinder($db); $users = $finder->findAllActive(); 

This ensures that you are not bound to a specific implementation of the Database class (you could make a sub-class) and will allow you to create separate UserFinders with separate Database instances. It will also make it easier to write tests for your application because you have less dependencies, which are not hidden and also replaceable.

In short: Use dependency injection.

Sign up to request clarification or add additional context in comments.

2 Comments

But i have 2 3 databases instance
Exactly, so one UserFinder instance is always tied to one database instance.
2

Since global variables are dirty (you always need the global $var; statement), the easiest solution is to store them in a static member of a class, e.g. Database::$db.

Another solution (in a proper OOP environment) would be passing the database instance to the classes - in your code it would be the constructor of one which accepted the instance and then stored in a private member variable.

2 Comments

Please check this question stackoverflow.com/questions/5324300/…
I see benefit in using static class that no need to pass the database object to each class what is the benefit in using oops conceps
2

Maybe, you should think about implementing your Database class according to a Singleton Pattern.


Updated (according to comment below):

Ok. I have only one suggestion here (except passing object via method's parameters and Dependency Injection, described in igorw's comment)...

Dependency Injection is a good way, but this case you - I suppose - have some small amount of databases, so it can be better to save them all in some static private array and get by keys.

So you will have only one public static method getInstance($key) and keys can be stored as some predefined constants (to avoid "spelling" errors).

This way you don't require initialization at all (getInstance($key) can create new Database objects with necessary parameters [passed to constructor] depending on the $key parameter). Dependency Injection looks better in general, but in some particular cases this way can be easier-to-use.

2 Comments

But i want multiple database instances , means i need to connect to multiple databases.
so then you create a generic database class that you can get / create new database connections.
1

Could be ok to have a Db setAdapter method that store database connections in a static property by name:

Db::setAdapter('db1', 'mysql:host=localhost;dbname=' . $dbname, $user, $pass); Db::setAdapter('db2', 'mysql:host=localhost;dbname=' . $dbname2, $user2, $pass2); 

Then a getAdapter method that will return a database connection when needed:

Db::getAdapter(); // return the default one Db::getAdapter('db2'); // return an instance by its name 

Behind the scenes you could implement a lazy connection too.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.