08.07.2007 / Object-oriented PHP :: A guide for fellow ISys junkies
Using a constructor
While PHP doesn’t require a constructor, you can still use one. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 | class MyClass { public $woot = "Woot!"; function __construct() { $this->sayWoot(); } private function sayWoot() { echo $this->woot; } } |
The function name __construct() that I used for the constructor was not chosen randomly. PHP requires that you name your constructor exactly like that. You may see code with a constructor that just uses the same name as the class (like in Java,) but that is now deprecated and has been replaced by the function name __construct(). That’s two underscores.
So, with our constructor set, we can now instantiate an object and it will automatically spit out Woot!.
1 | $myObject = new MyClass(); |
Notice that the constructor really just calls the sayWoot() function. It could’ve done echo $this->woot; itself, but I just wanted to let you see how to reference another class function. It’s pretty similar to referencing the class variables. Also notice that I declared the sayWoot() function as private, so it can’t be called from outside the class.
Passing arguments
So what if we wanted to pass a variable as an argument into a constructor or function? Let’s check it out:
1 2 3 4 5 6 7 8 9 10 11 12 | class MyClass { function __construct($whatToSay) { $this->saySomething($whatToSay); } private function saySomething($whatToSay) { echo $whatToSay; } } $myObject = new MyClass("Woot!"); |
It’s pretty similar to Java, so there’s not a whole lot that’s new. Again, the constructor could’ve echoed $whatToSay itself, but I just wanted to show you a little variety.
Passing by reference vs. value
There are two ways to pass an argument to a function: (1) by reference and (2) by value. Let’s look at a couple examples and then I’ll explain what’s going on.
Passing argument by value:
1 2 3 4 5 6 7 8 9 10 11 12 | class ValueChanger { public function changeTheValue($variableToChange) { $variableToChange = 'new value'; } } $valueChanger = new ValueChanger(); $myVar = 'old value'; $valueChanger->changeTheValue($myVar); echo $myVar; |
Passing argument by reference:
1 2 3 4 5 6 7 8 9 10 11 12 | class ValueChanger { public function changeTheValue(&$variableToChange) { $variableToChange = 'new value'; } } $valueChanger = new ValueChanger(); $myVar = 'old value'; $valueChanger->changeTheValue($myVar); echo $myVar; |
As denoted, the first example passes the argument by value while the second example passes the argument by reference. The difference in code is that the second example has an ampersand (&) right before the $variableToChange argument. That’s it. The difference in output is that the first example (passing by value) outputs ‘old value’ while the second example (passing by reference) outputs ‘new value.’ Why?
In the first example, when the changeTheValue() function gets called, PHP creates a copy of our $myVar variable and passes the new copy to the function; once we’re inside the function we’re no longer dealing with our original $myVar variable. Therefore, when we set $variableToChange (the copy that’s inside the function) to ‘new value,’ it doesn’t affect our $myVar variable (the copy that’s outside the function.) In the end, $myVar goes unchanged and our script outputs ‘old value.’
In the second example, when the changeTheValue() function gets called, PHP passes a reference of our $myVar variable to the function. This means, once inside the function, when we set $variableToChange to a new value, we’re also setting $myVar to that new value simultaneously; $variableToChange is merely a reference to $myVar. Due to the reference, $myVar gets set to ‘new value’ and therefore our script outputs ‘new value.’

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.