开发者

How should I use my database class in other class?

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

$liveresellerdb=new Database('host','user开发者_StackOverflow中文版','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    
    }
}


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜