Object-oriented PHP :: A guide for fellow ISys junkies
08.07.2007I’m currently studying Information Systems at Brigham Young University where the main language being taught to students is Java, a very object-oriented language. Some students, after having learned Java, have difficulty making the jump from Java to PHP for one reason or another. While I do not profess to have reached the level of Zen Master in PHP, I do hope I can provide some insight in a way that fellow ISys junkies can take what they’ve learned and pick up PHP if they want. As always, I’m open to improvements/corrections in my code, questions, complaints, I-could-do-it-better-than-yous, whatever. So here we go!
First off, PHP has not always been an object-oriented language and has yet to become a “pure” object-oriented language (and probably won’t ever be–it’s good at filling a distinct niche somewhere between scripting and enterprise application development). For example, you can still code in PHP without using objects at all. Also, some object-oriented features that are available in other languages are still not available in PHP, like overloading (although supported, it’s not implemented the “standard” way we’re used to in other languages) and namespaces. There are hacks that can allow for such functionality, but we’ll leave those alone for now. In any case, PHP is still my friend and it can be yours too.
Before we get into the OO (object-oriented) code, I want to give a really short intro to a few PHP basics. If you’ve already played with PHP, you’re probably safe to move onto the next page.
Defining a variable
In PHP, a variable is defined the following way:
1 | $myVar = "my string"; |
Notice I did not have to declare $myVar as a string before setting its value. If you were to then set $myVar = 6, no conversion code on your part would be necessary; PHP would convert it from a string to an integer for you. That’s one of the beauties and the uglies of PHP, take it or leave it. Every variable name must be preceded with a dollar sign (we’ll see somewhat of an exception on the next page.) PHP is also case-sensitive, so be sure you take care of capitalization.
Outputting to the browser
To output something on the user’s browser, use the echo command:
1 2 | echo "A test."; echo $myVar; |
The first line prints A test. while the second line prints whatever is stored in your $myVar variable.
Concatenation and variable interpretation
Because we precede every variable name with a dollar sign, PHP can easily tell what’s a variable and what’s not. Let’s look at some code:
1 2 3 4 5 | $name = "John Doe"; echo "Hello, $name! Welcome to my website.<br />"; echo "Hello, " . $name . "! Welcome to my website.<br />"; echo 'Hello, $name! Welcome to my website.<br />'; echo 'Hello, ' . $name . '! Welcome to my website.<br />'; |
This outputs the following:
Hello, John Doe! Welcome to my website.
Hello, John Doe! Welcome to my website.
Hello, $name! Welcome to my website.
Hello, John Doe! Welcome to my website.
In our code, using double-quotes tells PHP to interpret our variables and spit out their values. On the other hand, using single-quotes tells PHP not to interpret variables, which is why our third output line came out the way it did. Finally, using a period (.) lets you concatenate the same way that you would using the plus sign (+) in Java. Usually we’ll want to use our first method unless there’s a compelling reason not to.
Now to the fun stuff…


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.