Subscribe via RSS


Object Oriented PHP Part 2

Introduction

Welcome to Part 2 of a three part series introducing Object Oriented PHP! This tutorial will build off thefirst part from last week. Compare this to playing a sport; if you don’t know the basics, you’ll never progress.

In This Tutorial

Today, we are going to learn the following:

  1. Constructors and Destructors
  2. Returning data from your functions
  3. Keeping organized

Constructors and Destructors

Think of PHP constructors and destructors like a building. You construct a building at first, then when you’re done using it, you destruct it. Except in PHP, there are no live explosives for the destruction. Let’s look at the following example of constructors:

  1. class MyClass
  2. {
  3. function __construct()
  4. {
  5. echo "MyClass Loaded!";
  6. }
  7. }
  8. $MyClass = new MyClass();
  9. ?>

In this example, we create a new class simply called MyClass, then a constructor function that saysMyClass Loaded!. Basically, anything you want to happen when you call upon your class, should go in the constructor function.

In PHP, you don’t need to worry about always having a __destruct() method in your class, as all classes and variables are removed after your object is destroyed. (At the end of execution).

Returning data from your functions

In the real world, you don’t want to be using echo statements within your functions, you want to run your function and return the data for you to echo out where you want it. Let’s take a look at the following example that could be part of a blog application:

  1. class MyClass
  2. {
  3. var $mysqli;
  4. function __construct()
  5. {
  6. $this->mysqli = new mysqli('localhost', 'root', '', 'blog');
  7. }
  8. function get_latest_posts()
  9. {
  10. //Do some database selection
  11. $query = "SELECT * FROM `posts` ORDER BY `id` DESC";
  12. $result = $this->mysqli->query($query);
  13. return $result;
  14. }
  15. }
  16. php?>

Here, we are using a constructor to make a new MySQLi connection, and then using that connection set in the constructor to run a possible query, then to return the result set to be used however you’d like to display the data. Simple, right?

Keeping Organized

Thinking in the Box

In the first part of this series, I described classes as boxes, and functions as the things within the boxes. This picture is a great representation of keeping your classes well separated from each other, and their contents nicely arranged.

One key aspect of writing classes is to keep things easy to read and edit later. Let’s take a look at afile from the very popular blogging platform Wordpress. As you can see, above every variable declaration, class declaration, and function declaration, there is vital information about the parameters, what the function does, and what it returns. Let’s write our own example now:

  1. /*
  2. * @name MyClass
  3. * @params none
  4. * Our database class that makes a new database connection, and will insert userdata.
  5. */
  6. class MyClass
  7. {
  8. /*
  9. * MySQLi Connection Link
  10. */
  11. private $mysqli;
  12. /*
  13. * __construct
  14. * Sets new MySQLi Connection
  15. */
  16. function __construct()
  17. {
  18. $this->mysqli = new mysqli('localhost', 'root', '', 'buildinternet');
  19. }
  20. /*
  21. * insert_userdata
  22. * @params username, password
  23. * @returns bool
  24. */
  25. function insert_userdata($username, $password)
  26. {
  27. //Insert the userdata into the database
  28. if(success)
  29. {
  30. return true;
  31. } else {
  32. return false;
  33. }
  34. }
  35. }
  36. php?>

Here, we have said what our class is, what it does, and then defined each function or variable within it. In the insert_userdata() function, we have said what the parameters are and what it returns above the function. As you can already see, these comments help immensely when trying to read your code or trying to find a problem with your code.

Conclusion

Now, after reading the second part in this series, we have all the skills we need to write our MySQLi Database interaction class in Part 3! Thanks for reading and be sure to check back for the third part!


Posted by ABDUL SABOOR Tuesday, October 6, 2009

0 comments

Post a Comment