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:
- Constructors and Destructors
- Returning data from your functions
- 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:
- class MyClass
- {
- function __construct()
- {
- echo "MyClass Loaded!";
- }
- }
- $MyClass = new MyClass();
- ?>
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:
- class MyClass
- {
- var $mysqli;
- function __construct()
- {
- $this->mysqli = new mysqli('localhost', 'root', '', 'blog');
- }
- function get_latest_posts()
- {
- //Do some database selection
- $query = "SELECT * FROM `posts` ORDER BY `id` DESC";
- $result = $this->mysqli->query($query);
- return $result;
- }
- }
- 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
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:
- /*
- * @name MyClass
- * @params none
- * Our database class that makes a new database connection, and will insert userdata.
- */
- class MyClass
- {
- /*
- * MySQLi Connection Link
- */
- private $mysqli;
- /*
- * __construct
- * Sets new MySQLi Connection
- */
- function __construct()
- {
- $this->mysqli = new mysqli('localhost', 'root', '', 'buildinternet');
- }
- /*
- * insert_userdata
- * @params username, password
- * @returns bool
- */
- function insert_userdata($username, $password)
- {
- //Insert the userdata into the database
- if(success)
- {
- return true;
- } else {
- return false;
- }
- }
- }
- 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!
0 comments