Simple Model CRUD with PHP 5.3

I’ve been wanting to mess around with some of the new features in PHP 5.3, so I took the opportunity to write a base model class that can be used for simple db based models. This isn’t really production type code, more of an example of “look what php can do now”, although with some error handling and tweaking, it might be a good start to a lightweight active record type base class.

One of the biggest new changes to 5.3 is late static binding. It has never been possible in PHP to get the name of the calling class when you called a static method inherited from a parent. Now with late static binding, this is possible. This is what works the “magic” in this base class.

The Assumptions

Models represent an entity stored in the database (think active record):

  • In one table per class named the same as the model class, but all lower case
  • With one column for each property
  • With a primary key column named ‘id’

In addition, a global variable $db should contain a PHP PDO object.

The Base Class

One of the things I’d like to be able to do is create an object from an array of parameters, whether they come from a database or not. I might want to create from a set of parameters and be able to act on it immediately, without having to reload it. I use reflection here to ensure that all the class properties are set, throwing an exception if any are missing.

Note also in the get and getAll methods, the calls to new static(). What this does is create an object of the class you are calling, rather than the class that the method exists in. This wasn’t possible until PHP 5.3, and is really the key to being able to use static inheritance.

In addition, there is a lambda function in the save method, this is new to 5.3 also. And there are a few examples of a new php function “get_called_class()” which will return the name of the class that was called.

 

Now, you’ll probably notice that this is an abstract class, meaning that we can’t create any Base objects, but must extend the class. Here is an example of a child class that holds name / value pairs:

Once I’ve created the matching db table, I’m ready to get some CRUD done.

Creating an object

To create an object, I simply pass the parameters as an associative array of properties and their values.

Getting objects

Getting is simple. Either get by ID or get an array of all the objects. It would be fairly trivial to add a find method.

Updating

You can update multiple parameters at once by passing an associative array as you did in the create method, or update a single parameter by using the updateProperty method and passing the name and value.

Deleting

 

6 Responses to “Simple Model CRUD with PHP 5.3”

  1. Eric L Says:

    Hi,

    Nice piece of code! I will probably try it but for now there is some little things I don’t understand well…

    In the delete() function, why are you storing your your SQL statement in a $delete variable that you never use again. Maybe the next line should be :

    $statement = $db->prepare($delete); “instead of : ” $statement = $db->prepare($statement);

    If possible I would also know more about the global variable $db that should contain a PDO object. I read some tutorial about PDO objects like the one at : “http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html” but they don’t use it the same way.

    Thanks you,

    Eric

  2. Andy Says:

    Hey,

    This is awesome, I’ve been looking for something with reflection in it for ages! I’m a little confused with what the fetchColumns() function does. You’re calling it on the table representation, but that is not documented here.

    Please let me know.

    Andy

  3. Moroswitie Says:

    Nice post,
    will give this a try and adjust a couple of small things.
    How would you go about having a dynamic “id”
    could you pass in an extra parameter, so that for different models you could use:
    car_id, user_id, etc. (instead of a fixed ‘id’ field)

    Note: you shouldn’t use MAX(id) to get the current highest ID, use last insertID.

  4. TJ Says:

    Moroswitie that makes sense, just pass it from your extended class.. Great post :)

  5. canadadeals.ca Says:

    Great code thank you.

  6. sara Says:

    Hello, thanks for sharing.
    I’ve tried to create an object by just copying ur code (i’ve include all the necessary files and connect my db and have an instane of a PDO file) but it keeps showing:


    Fatal error: Call to a member function fetchColumn() on a non-object in C:\Program Files\EasyPHP-5.3.9\www\base.php on line 140 ”

    I would like to give a remarq about the delete function:

    public function delete()
    {
    global $db;

    $delete = “DELETE FROM ” . strtolower(get_called_class()) . ” WHERE id = :id”;

    $statement = $db->prepare($statement); // here it should be $delete instead of $statement : $statement = $db->prepare($delete)

    ………………………..