开发者

Proper use of database class in PHP?

开发者 https://www.devze.com 2023-02-20 15:02 出处:网络
I\'ve written a fairly simple database class in PHP that supports the most basic 开发者_JAVA技巧stuff such as connecting/disconnecting, querying, fetching data etc.

I've written a fairly simple database class in PHP that supports the most basic 开发者_JAVA技巧stuff such as connecting/disconnecting, querying, fetching data etc.

I know I'm reinventing the wheel here but there is some learning aspects behind this. I've tested it out and it works well. Now I want to make use of this class for a website I run that I know have a fair amount of simultaneous users at any given time and I found myself somewhat uncertain as to how to use the class.

Do I create a new instance each time I want to use the class? Lets say I have a function that lists something i my database, do I do something like this?:

function listStuff() {
    $db = new Database();
    $db->connect();
    $result = $db->query($someQuery);
    //do stuff with result
    $db->dicsonnect();
}

This seems to create an unnecessary amount of objects in memory, so how would I go about it to reuse objects but still be able to have simultaneous users?

Thanks!


You should be using dependency injection. This will ensure your code is not only easy to read and testable, it'll make your functions (which I would hope are in classes) reusable.

function listStuff(DatabaseClassInstance $db) 
{
    $result = $db->query($someQuery);

    //do stuff with result
}

The $db object will be terminated when the script end if you are using the MySQLi or PDO extensions.

The type hinting DatabaseClassInstance isn't necessary but is nice in this case to ensure that the correct parameter type is being passed into the function.


well you cannot use a single instance across more than one user. but for a single user you can prevent unnecessary instantiation by passing an instance of that object to a central repository, called registry.


Strictly speaking, you wouldn't be creating a single instance for multiple users. Every request that constructs the Database object is instantiating it again.

If you are primarily retrieving user info utilizing the database, I would recommend writing an object model to represent your users. This, as well as your database class, could be included in the global scope along with a call to session_start() to allow you to manage user sessions in one class interface. It becomes much easier to model site features when persistency managed by the database is wrapped within some object interface that makes sense.

You could write a basic class that has methods that utilize your database class and feed queries into it.

class User{
  public function __construct($user_id){
    //use your database class to find a user id and assign properties of the user data
    //Then save to the session.
    $_SESSION['user'] = $this;
  }

  public static function Login($username, $pass){
    //use your database object to find the user id associated with the passed login creds,  then construct it as a user using the above function;
  }


  public static function Logout(){
    //remove any user in the session and restart the session
  }
}

This kind of model based code is often useful for creating very semantic method names, and it can be useful for managing global state, such as user properties in the database. It also comes in handy when you start writing abstract classes that contain connectivity and structuring for the data that children can inherit from.

I've run into alot of database classes that are deployed as singletons when small deployments are all that is necessary, and a call to any of their query function creates accesses a persistent connection and then closes it at the end of the request.


global $db;

lorem ipsum dolor sit amet

function listStuff() {
    global $db;
    $result = $db->query($someQuery);
    //do stuff with result
}
0

精彩评论

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

关注公众号