An Introduction to Object Oriented PHP – Part 3

An Introduction to Object Oriented PHP – Part 3

Introduction

Welcome to the third and final part of a series introducing Object Oriented PHP! Please, as I have urged before, go back and read parts one and two if you have not already, the basics are the most important part!

In this Tutorial

Today, we are going to finally complete the task that we’ve been leading up to: writing our MySQLi DB class! We’re going to be using everything we’ve learned so far to do this, so strap in and enjoy!

  1. Variables and Constructor
  2. Main Functions
  3. Some extra goodies

Variables and Constructor

Create a new file called class.db.php and insert the following:

**Try and dissect what we have done here before reading the explanation!**

First, we have done the most important part of Object Oriented PHP – organization! We have said what the class name is, and all the parameters that need to be passed to it when it is loaded. Next, inside the class, we have defined our four variables for MySQL connection and then required them as parameters of the constructor function, which can be passed when the class is loaded at the bottom of the file. Next, we have set our $mysqli variable to a new MySQLi Object. Simple, right? Let’s move on.

Main Functions

SELECT

Now that we have the connection down, we can start working with the database. Add a select function like this:

Here we have made a select function for selecting data from a MySQL table. We have defined three parameters, and you can see how they fit into the final query. All we return from this function is a query result set, which you can work with however you like in your pages.

INSERT

Of course, we can select data with our class now, but we don’t have anything to select unless we insert it! Let’s do that now by adding this function to your class:

Another quite simple function, we are just requiring two parameters: the table name to insert the data into, and the values for the fields. Instead of returning any real data for this function, we just go ahead and run it and if it inserted the data, it returns true, and if not, false. Simple for inserting data.

DELETE

Now that we can insert and then select our data, we have to have a way to delete it say if a user wanted to delete their account. Add this function:

Some extra goodies

You probably noticed the last_query variable we defined at the beginning of the class, then we set it every time we ran a query in a function. This is very vital for troubleshooting, to see what’s wrong with your query. Another possible class variable could be a last_error, that would hold the last error returned.

Conclusion

This will conclude this series on Object Oriented PHP – I hope you’ve at least learned the basics so you’ll be able to power up your applications! Thanks for reading.

Posted Thursday, July 30th, 2009 · Back to Top

SPONSOR

Add Comment

22 Comments 6 Mentions

  1. Chukki Author Editor

    Very nice job! I love the OOP Series… because its a hole in my skills…! More like this ;)

    ·

  2. Paratron Author Editor

    I prefer to give the “Insert” Function an assoziative array and it has to build the MySQL-Insert String for itself. Thats way clearer, as one often gets confused by complex Insert Actions!

    You can have a look at an example class here -> http://parastudios.de/labs/gear/pmysql.php.txt
    (Look for the function makeSqlValueString() at the bottom).

    Sorry, the documentation is in german ;)

    ·

  3. Jenna Author Editor

    In your __construct() shouldn’t it be ‘$this->host = $host;’ etc. instead of ‘$host = $this->host;’?
    .-= Jenna´s last blog ..JJenZz: RT: @chillyjames: Lunascape – world’s first triple engine browser http://bit.ly/Mp7hP =-.

    ·

  4. Jack Franklin Author Editor

    Your construct function (I think) is wrong:
    $host = $this->host;
    $user = $this->user;
    $pass = $this->pass;
    $name = $this->name;

    Should it not be the other way round?

    ·

  5. emilien Author Editor

    I think your class is too specific. It must be a factory patern to contruct a db abstraction layer with abstract method. I can’t use your class to make a db mysql class… You have to create other class named db_Sqli or db_Mysql for that… It’s the real goal of oop…

    ·

  6. Phunky Author Editor

    Use of magic methods such as __get() and __set() would be ideal within you __construct() which would allow you to __set(‘host’, $host);
    .-= Phunky´s last blog ..Did i forget something? =-.

    ·

  7. GiN Author Editor

    Jack Franklin is right. But Phunky is not. Because use a magic methods is not a best way.
    .-= GiN´s last blog ..Что делать если усы и борода мешают партнеру? =-.

    ·

  8. Martin Bean Author Editor

    Jenna beat me to it: your __construct() method is wrong because you should be assigning the method’s passed arguments to the class’s properties, but you have it doing it the other way around, overriding the class’s properties with your __construct() method’s arguments.

    ·

  9. Benjamin Reid Author Editor

    Nicely explained. I was working on a database class myself the other day that’s very similar!
    .-= Benjamin Reid´s last blog ..Quick Tip #5 – A ‘Hello world’ introduction to PHP classes =-.

    ·

  10. Cody Robertson Author Editor

    Hmm, you seem to go a bit far on user friendlyness.

    I always define my variables then just user the construct function to connect to mysql.

    Then again, it should be,

    $this->host = $host;
    etc.

    ·

  11. kodegeek Author Editor

    Nice Post. Love OOP as well.

    ·

  12. Karol Sójko Author Editor

    It is always a good practice to make prepared statements and then bind the parameters, as all data coming from users should be treated as tainted.

    Good work with the blog,
    Cheers :)
    .-= Karol Sójko´s last blog ..Ubuntu tutorial: How to install Tweetdeck on Ubuntu 9.04 64 bit =-.

    ·

  13. ivicta Author Editor

    wrong !

    it’s :

    $this->host = $host;
    $this->user = $user;
    $this->pass = $pass;
    $this->name = $name;

    not

    $host = $this->host;
    $user = $this->user;
    $pass = $this->pass;
    $name = $this->name;

    ·

  14. Dixon Crews Author Editor

    @ivicta – It works either way.
    .-= Dixon Crews´s last blog ..dixoncrews: Is it me or is Verizon getting a *little* cocky? http://phones.verizonwireless.com/motorola/droid/ =-.

    ·

  15. Data Skull Author Editor

    This database class needs a lot of work, but it is a good introduction to OOP PHP. Without the DB class I currently use in my projects I would be lost. It’s amazing how many lines of code a DB class will remove.

    ·

  16. malisa Author Editor

    hey thanx for an informative tutorial ….helped a lot

    ·

  17. Jason Davis Author Editor

    I love the site, keep up the great work! Another note, the use of
    var $host; is the depreciated way of doing it, you should use public/private $host; instead.

    ·

  18. Thinkers Author Editor

    Hi
    Good post.
    But could you (or any one) please send me this code in zip format. In fact I am a beginner and having a lot of problems to use this code in my project.

    ·

  19. Nathaniel Author Editor

    This introduction is very detail, thank you.

    ·

  20. Free Logo Design Author Editor

    The functions described are informative. This is well organized and resourceful tutorial about php.

    ·

  21. Free Vactor Download Author Editor

    This site is informative and guide to solve our probem

    ·

  22. nish Author Editor

    how wud u pass values to insert function..

    ·

 

Build Internet by One Mighty Roar. Since 2008.