So far throughout this tutorial, we’ve been looking at a method of programming called “procedural programming” in which program execution proceeds in a linear fashion, from start to finish and makes use of procedure calls. Procedures, which we’ve come to know as functions, allow us to execute arbitrary code segments at any point throughout the script.
Procedural programming has many advantages, mainly the ability to store frequently used code within functions, and to encompass complex algorithms within easy-to-remember function names.
In this tutorial, I’ll take things a step further, and introduce you to the concept of Classes and Objects in PHP. To put it simply, an Object in the context of programming languages is a data structure which can contain any number of variables and functions all held together under a Class name. This Class can then be created in the normal scope of the code and stored inside a new variable, and multiple copies of this same class can be created. This allows for an even greater level of code encapsulation, allowing more complexity to be accessed from simple commands.
I mentioned that procedural programming allows us to encapsulate code within functions to be re-used as required. Classes take that a step further, allowing us to encapsulate many functions within a class and to create multiple copies of each class.
Note that, when inside a class definition, functions are referred to as methods, and variables are referred to as members - even though we still use the keyword “function” to define methods.
When defining a function within a class, we refer to it as a method, and this method then becomes available to all instances of a class. The following example shows the syntax used to define a class, and also create an instance of the class for use. We will then call the method defined within the class, using this newly created instance
Class MyClass { function myFunction(){ echo "Hello world!"; } } $myObject = new MyClass(); //create an instance of MyClass called $myObject $myObject->myFunction(); //call the method, myFunction() using the instance
Lets take a step by step look at what’s happening here
- Line 1 : New classes in PHP are defined by the use of the Class keyword, followed by a user defined name to label the class. Here, we’re calling our class MyClass. The body of the class is placed between a pair of braces. Class naming convention is the same as for standard variables and function names, and traditionally class names start with an uppercase letter
- Lines 2-4 : Within the class, we can define a range of variables and functions. Here, we’re defining just one function, in this case, the function myFunction, which serves a single purpose - to print “Hello world!”
- Line 6 : Here’s where the fun begins! After a class has been created, we can then assign instances of the class to variables. Here, we make use of the “new” keyword to create a new copy of the MyClass object. Note the parentheses after MyClass. These aren’t explicitly required here, but later on in the tutorial, I’ll show you how you can place values within these to add extra functionality to your class.
- Line 7 : by the use of the -> operator we can call functions and access variables within the class object. Here, we are calling the myFunction function. This will produce the now-familiar output
Hello world!
Multiple instances of the same class can be created. This way, we can reuse the functionality of the class without needing to worry about redeclaring variables, or functions
$firstObject = new MyClass(); $secondObject = new MyClass(); $thirdObject = new MyClass(); $firstObject ->myFunction(); $secondObject ->myFunction(); $thirdObject ->myFunction();
Let’s take a step forwards now and add in some variables, or members into our functions. When defining variables within the body of a class, we refer to these variables as members. Here is an example of another class definition with a couple of members, $a and $b defined within its body.
1 2 3 4 5 6 7 8 9 10 11 12 | Class MyClass { public $a = 5; public $b = 10; function showValues(){ echo "a equals $this->a"; echo " and b equals $this->b"; } } $class = new MyClass; $class->showValues(); echo "a plus b = ".($class->a + $class->b); |
a equals 5 and b equals 10 a plus b = 15
Here, you’ll see much that is familiar from the previous example. We define a class using the “Class” keyword, and inside the class is a function, or method showValues(). But, we’ve got a couple of new things here that need some explaining!
- Lines 2 and 3 : Here, you can see a couple of members $a and $b being defined, in much the same way as we’d define variables within the normal scope of a script, except when defining members inside a class, we must use the public keyword. There are exceptions to this rule though, and they will be explained shortly. Note that it is also acceptable to define members by using the keyword var e.g. var $a = 5; although this usage is a remnant from earlier versions of PHP and is only supported for compatibility reasons. It is better to use public
- Lines 6 and 7 : Now that we have the variables $a and $b defined within the body of the class, we can use these values from within functions. Here, you’ll see another mysterious variable - $this. $this is a very special variable in PHP. When inside a class, or object, $this allows you to refer to the class from within itself. Notice how, when calling a class method from outwith the class definition we use the notation $class->functionName()? When we’re running code from inside the class itself, we can do the same thing, but instead of using the name of the class, we simply use $this. This way we can refer to the class object without needing to know the specific variable name the class has been assigned to.In this case, we’re working with the values of $a and $b from inside the method by referring to them with $this->a and $this->b
- Line 12 : Here we are accessing the values of $a and $b directly. You can see from this example how it’s much cleaner, and compact to use methods from within the class to access variables in this way, since it allows for greater code portability. This code can be improved like so,
Class MyClass { public $a = 5; public $b = 10; function addValues(){ return "a plus b = ".($this->a + $this->b); } } $class = new MyClass; echo $class->addValues();
Confused? Yes, don’t worry, it can be a little overwhelming at first, but the thing to remember about Object Oriented Programming is that it uses lots of the same principles you’ve been using already, but with a little twist. It’s really about changing the way you think about coding, into a more packaged, compartmentalised view of things.
In the next section, we’re going to take a look at how we can control access to the members and methods contained within a class




September 19th, 2008 at 5:21 pm
Thank you very much for this tutorials. Just learning PHP and I’ve progressed to OOP in PHP. Will you be writing more stuff, especially on abstract methods and interfaces? I can’t for the life of me understand abstract methods and when they would be needed. I have read about 3 different tutorials but still get nada, lol. I like your tutorials, they are easy to understand so that’s why am looking forward to some tuts from you with respect to abstracts and interfaces. Thanks again!
September 19th, 2008 at 5:26 pm
Yes, I’ll hopefully be going deeper into the workings of Class based programming in PHP in the near future, probably with some code samples as examples in the “PHP/MySQL Code Samples” section. Glad you’ve found the tutorial helpful! Keep an eye on the site, I should be able to add some new tutorials over the next few days.
September 19th, 2008 at 5:46 pm
Nice one! I have already bookmarked the site and will definitely keep an eye here and recommend it to my mates!
cheers/k
December 5th, 2008 at 2:55 am
Thanks for a great tutorial. Like kongondo I can’t wait for some more PHP oop tutorials.
December 30th, 2008 at 1:55 am
Great!, you explained clearly OOP programming in PHP than other tutorial’s I had read. This article give some light for me about OOP.
hope to see more from you…
cheers!