Subscribe via RSS


OOP Introduction to 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:

  1. /*
  2. * class db
  3. * @param Host
  4. * @param User
  5. * @param Password
  6. * @param Name
  7. */
  8. class db
  9. {
  10. var $host; //MySQL Host
  11. var $user; //MySQL User
  12. var $pass; //MySQL Password
  13. var $name; //MySQL Name
  14. var $mysqli; //MySQLi Object
  15. var $last_query; //Last Query Run
  16. /*
  17. * Class Constructor
  18. * Creates a new MySQLi Object
  19. */
  20. function __construct($host, $user, $pass, $name)
  21. {
  22. $host = $this->host;
  23. $user = $this->user;
  24. $pass = $this->pass;
  25. $name = $this->name;
  26. $this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->name);
  27. }
  28. }
  29. $db = new db('localhost', 'root', '', 'blog');
  30. ?>

**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:

  1. /*
  2. * Function Select
  3. * @param fields
  4. * @param from
  5. * @param where
  6. * @returns Query Result Set
  7. */
  8. function select($fields, $from, $where)
  9. {
  10. $query = "SELECT " . $fields . " FROM `" . $from . "` WHERE " . $where;
  11. $result = $this->mysqli->query($query);
  12. $this->last_query = $query;
  13. return $result;
  14. }

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:

  1. /*
  2. * Function Insert
  3. * @param into
  4. * @param values
  5. * @returns boolean
  6. */
  7. function insert($into, $values)
  8. {
  9. $query = "INSERT INTO " . $into . " VALUES(" . $values . ")";
  10. $this->last_query = $query;
  11. if($this->mysqli->query($query))
  12. {
  13. return true;
  14. } else {
  15. return false;
  16. }
  17. }

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:

  1. /*
  2. * Function Delete
  3. * @param from
  4. * @param where
  5. * @returns boolean
  6. */
  7. function delete($from, $where)
  8. {
  9. $query = "DELETE FROM " . $from . " WHERE " . $where;
  10. $this->last_query = $query;
  11. if($this->mysqli->query($query))
  12. {
  13. return true;
  14. } else {
  15. return false;
  16. }
  17. }
  18. ?>

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 by ABDUL SABOOR Monday, October 5, 2009

3 comments

  1. stymiee Says:
  2. This code is obsolete! It's written in PHP 4 which isn't even supported by PHP anymore. It should be written in PHP using private/protected/public instead of var.

     
  3. Xycaleth Says:
  4. It's written in PHP5 - the __construct function doesn't exist in PHP4.

     
  5. Aaron Godin Says:
  6. Actually you are both right. However, the post should be updated to use public, protected, or private for variable declarations because that is what is supported in PHP5.

     

Post a Comment