1

I've written a fairly simple database class in PHP that supports the most basic 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!

2
  • 1
    avoid creating stuff on your own. Don't reinvent the wheel. use a framework, zend, yii, sphmyfony Commented Mar 28, 2011 at 23:34
  • @yes123 avoid creating stuff on your own xD He shouldn't create it if he doesn't know what he is doing. Kohana FTW. Commented Mar 28, 2011 at 23:59

4 Answers 4

4

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.

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

1 Comment

+1 as Dependency Injection is the best way to go as opposed to using global. Also, @Slim you aren't really re-inventing the wheel. At my work we also use a DB class that handles master/slave connections as well as helper functions such as querying by first row, querying by id and the standard query objects and arrays, etc. Using pre-existing frameworks do work but you need to know what you're doing with them and most of the time you also need a lot of their package just to use their database classes which adds more overhead than you may want, or need.
0

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.

Comments

0

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.

Comments

-1
global $db;

lorem ipsum dolor sit amet

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

8 Comments

global $db; $db = new Database(); $db->connect(); function listStuff() { $result = $db->query($someQuery); //do stuff with result } $db->dicsonnect();
Wouldn't that cause synchronization problems? Lets say one user1 is doing a connect and then performing a query, but between the connect and the query user2 is doing a disconnect. Or am I missing something?
globals are evil and shouldn't be used, especially not in this context
Just going with the flow... @Slim No, every user has it's own connection, 100 users, 100 connections, your example would make something like X connections for 100 users. You don't need to disconnect.
@Slim: Each page request starts a distinct PHP processs. No objects are shared between them. So no two users are influencing the object instances of each other.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.