Subscribe via RSS


An Introduction to Object Oriented PHP

About This Series

This is a three part series introducing Object-Oriented PHP, a way to manage your code and keep different parts separate, all while being easily accessible. I hadn’t really planned it this way, but myprevious article works as a great precursor to this article, showing you how to get a PHP/MySQL Sandbox up and running, which is just what we’ll be using for this tutorial as I believe a hands-on approach is the best way to learn things like this.

By the end of this series, we are going to make a very simple MySQLi (MySQL Improved) database interaction class, for doing common tasks.

In this Tutorial

Today, we are going to learn the following:

  1. What the heck are Objects and Classes?
  2. Writing our first class
  3. How to use your freshly written class
  4. How to personalize it

Let’s get started!

What the heck are Objects and Classes?

Classes, at their simplest point, are just receptacles of functions. They can be compared to a folder on your computer (assuming you aren’t running DOS). Inside the folder you may have three files, or in this case, functions. Let’s use a classic example, a Dog.

Take a look at the following image:

A Canine Example

As you can see, a Dog has many “functions”. It can run, walk, sit, play, and bark. This is the essence of what a class is.

An Object, on the other hand, is a way to represent your class. For example, you can have your class named “Dog”, but you can reference it by a variable called Cat if you really wanted to confuse yourself.

That’s all we’re going to cover in this section, so if you’re still confused, read on and I believe the examples will make some coherent sense.

Writing our first class

Since this is just our first class, we’re going to keep things nice and simple. Open up your text editor of choice and make a new file in your web root called myClass.php. Add in the following code:

  1. class myClass
  2. {
  3. function sayHello()
  4. {
  5. echo "Hello there!";
  6. }
  7. }
  8. ?>

If you navigate to this file on your server, nothing will show up. All we did was set up a class (container) and add in a function, we never referenced it to be run.

How to use your freshly written class

Now that we have our myClass.php file all written, we’re going to reference it and use it. Make a new file in the same folder as your class is in, called index.php. Add in the following code:

  1. require_once('myClass.php');
  2. $myClass = new myClass();
  3. $myClass->sayHello();
  4. ?>

If you run the file, you should see “Hello World!” echoed out. Let’s go over what we did to make this work. First, you can see that we required the myClass.php file, which contains our container full of functions. Next, we had to make a new Object, and we used the class name as the object name, just for ease of use. And finally, we referenced our sayHello function by writing the Object name with an arrow and the function name.

Making it Friendlier

Right now, if we wanted to use this to greet a logged in user, there wouldn’t be much of a sense of personalization, would there? Wouldn’t it be nice if we could say Hello to the specific user? Well, we can! Go back to your myClass.php file and edit it accordingly:

  1. class myClass
  2. {
  3. function sayHello($user)
  4. {
  5. echo "Hello " . $user . "!";
  6. }
  7. }
  8. ?>

All we did was add a parameter to the function called name, that we can pass along to the function from our index file. Go back to your index.php file now and add your name like so:

  1. require_once('myClass.php');
  2. $myClass = new myClass();
  3. $myClass->sayHello('Dixon');
  4. ?>

Now, reload your index.php file and you should see a nice textual greeting!

Conclusion

Now that we’ve covered the very basics of Object-Oriented PHP, you can practically do it all. And was it hard? Hopefully not! Be sure to check in later for the rest of this series, in which we’ll be making a fully functional MySQLi Database interaction class! Thanks for reading.


Posted by ABDUL SABOOR Tuesday, October 6, 2009

0 comments

Post a Comment