开发者

Load pdo object in a class php

开发者 https://www.devze.com 2023-04-09 11:59 出处:网络
I have a single file I have my database connection (PDO) in. And as PDO is a class (library) or whatever, I don\'t feel the need of a database class (Or should I build one?) Anyway I want to load the

I have a single file I have my database connection (PDO) in. And as PDO is a class (library) or whatever, I don't feel the need of a database class (Or should I build one?) Anyway I want to load the pdo object, so I can use the functions in another class, but I'm not sure of how to do it.

pdo_object.php

//single php db connection file here..
$dbh = ...

user.php

class User {
    function login()
    {
        //do pdo query here...
   开发者_开发百科 }
}

Do I need to inject the $dbh on every single class that needs database connection?


"I don't understand your question. In User->login(), just put your PDO logic." - Don't I need to load in the dbh somehow in the class?

There is no single answear to that! If your class is going to execute the logic (practically speaking doing an mysql_query) then you had to provide the connection handler.

On the other hand your class does not execute the query but does for example some tasks like these: bulding the queries, applying the logic, checking if query data given by user is correct etc then no need to load the handler cause an other class will need it.

I still believe these kind of questions (about designing) are very tricky and philosophic, general etc unless they are very specific, and yours is not that much.

And as PDO is a class (library) or whatever, I don't feel the need of a database class (Or should I build one?)

In practice we do build a database class and not just for puting the pdo handler there only, but because there are so many things that can be defined apriori and help you a lot. You might have more than a database handler (one for public use and for internal for example), you might have your own methods check this from an old project:

public  function getValueLike($dbh, $table,$col_name, $value )
    {     

                $col_name = '`'. $col_name . '`';
                $sql = 'SELECT '. $col_name .' FROM '. $table .' where  '. $col_name ." like ? limit 2" ;

            //prepare,execute
            $stmt =  $dbh->prepare($sql );
            $stmt->execute(array('%'.$value.'%'));

            //we want it is an array with int keys
            $stmt->setFetchMode(PDO::FETCH_NUM); 
            $tmp =   $stmt->fetchAll(); //get all values

            $res[] = $tmp[0];//get the first value
            $res[] = count($tmp);//get the count

            //return the 1st value found, also the count which at most can be 2 
            return  $res ; 
    }

all these things including usernames, passwords, database servers etc etc wouldn't it be nice to be in one place (instead of being spread around), under a class that is easilly accessible, can apply access restriction etc. These are some arguments to justify my point.

If you decide to build a class you call the class only once at the very begining and then just pass the object around.

  • Go for MVC, choose a good one, the structure is mostly predefined and it will force you apply some cool stuff and make you a better programmer.

  • As for database handler I suggest as a minimum that you make it a singleton (static object) and pass the object around whenever needed an even better solution is to go with the non static way using a registry. Where you only pass the registry object somewhere and everything you need is accessible. If you cannot go into MVC just try to build a custom registry it will save a lot of time. There are good tutorials out there!!

0

精彩评论

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

关注公众号