08.07.2007 / Object-oriented PHP :: A guide for fellow ISys junkies
Before we hop into objects, you must be made aware of the var_dump() function. Basically, once you have an object variable (or any variable for that matter), you can see everything inside it like this:
1 | var_dump($myObject); |
Just a tip for debugging.
Our first class
To start off, let’s create our first class.
1 2 | class MyClass { } |
Yup, that’s it. But we can’t do much with it, so let’s add a class variable and a function.
1 2 3 4 5 6 7 | class MyClass { public $woot = "Woot!"; public function sayWoot() { echo $this->woot; } } |
In this case, we have a class variable named $woot and a function named sayWoot(), both of which are declared public, meaning they are accessible from outside the class. Notice that inside the sayWoot() function, we reference the class variable using $this-> and then the class variable name without the dollar sign. The only case that I can think of where a variable name is not preceded by a dollar sign is when you are referencing a class variable like we are here. Note that $this-> is similar to this. in Java. It’s a pseudo-variable that says “reference myself” if you’re inside an object. There’s also an identifier self::, which is used in a non-object context. To make the connection from what you learned in INTEX II, a BO would be an “object context” where you instantiate a container of information, move it around, etc. On the other hand, a DAO would be a “non-object context” where you just want to call one function and be done with it; there’s no reason to instantiate it as an object.
Since the writing of this tutorial, it’s been brought to my attention that we didn’t use static methods in our DAO class during INTEX. Still, I prefer using static methods. See the comments at the very bottom of this page for more info.
We’ll learn about self:: later on; just know that both exist and that there’s a difference between the two.
Now, let’s instantiate (make an object from) the class and do something with it:
1 2 3 | $myObject = new MyClass(); echo $myObject->woot; $myObject->sayWoot(); |
$myObject is now an instance of the MyClass class. Both our second and third lines of code output Woot! as you might expect. On the second line, we had to use the echo command because we only had the woot variable value and we wanted to spit the value out to the browser. On our third line, we did not have to use the echo command because we called the sayWoot() function, which contained its own echo command internally.
Returning values
What if we want to return a value from our function? Let’s tweak our class a little bit:
1 2 3 4 5 6 7 8 9 10 11 | class MyClass { public $woot = "Woot!"; public function sayWoot() { return $this->woot; } } $myObject = new MyClass(); $returnValue = $myObject->sayWoot(); echo $returnValue; |
As you can see, all it really takes is the word return and then whatever you want to return. No other changes are necessary. One important thing to notice is that nowhere do we declare the type of variable that we’re returning. This means whenever we call our function we must understand exactly what type of variable it’s returning so we can deal with it appropriately later on down the line.
One other small note. If I wanted to immediately output to the browser whatever the function returned, there’s no need to store the return value into a variable like I did. Instead, we could have just as easily said:
1 2 | $myObject = new MyClass(); echo $myObject->sayWoot(); |
Separating your code
In case you’re wondering, you can put both your class code and your non-class code in the same file (you can also put the code for multiple classes in the same file.) For example, I have a file called index.php that has the following:
1 2 3 4 5 6 7 8 9 10 11 | class MyClass { public $woot = "Woot!"; public function sayWoot() { echo $this->woot; } } $myObject = new MyClass(); echo $myObject->woot; $myObject->sayWoot(); |
On the other hand, when you actually start making a real application, you’ll want to split the code into different files. So index.php would have the following:
1 2 3 4 | require_once($_SERVER['DOCUMENT_ROOT'] . "/MyClass.php"); $myObject = new MyClass(); echo $myObject->woot; $myObject->sayWoot(); |
Note that the require_once() function requires the full file path to the file you’re including. $_SERVER['DOCUMENT_ROOT'] will retrieve everything in the path down to the root directory of your website, so that helps out. MyClass.php would have:
1 2 3 4 5 6 7 | class MyClass { public $woot = "Woot!"; public function sayWoot() { echo $this->woot; } } |

Nice site you have here Aaron! There’s just one small point that I think you might be interested in. You mentioned that PHP doesn’t support object overloading, but it actually does. Several special methods can be set up on objects, including __get, __set, and __call. This may not be the same implementation as is found in other languages, but it is overloading. Check it out.