Introduction to Object Oriented Programming with PHP

PHP is a very nice scripting language, relatively easy to learn and to develop in. But it’s not just a scripting language. Thanks to PHP’s Object Oriented capabilities you can save yourself a lot of work, encouraging code re-usability.

So how do you get started with Object Oriented Programming (OOP)? It would be quite difficult without reading a fair bit about the general principles of OOP, if you don’t have any background in OOP. For me it took a course in OOP with C++ (I couldn’t find any course in OO PHP), but believe me, after C++ PHP is really easy :-).

What is a Class? What is an Object? What’s a Method?

These three definitions are pretty crucial to understand if you even dream of getting into OOP (in any language, not only PHP). To simplify things a bit, a Class is like a code library that contains its own variables and functions. The goal of a class is to collect code that has very similar purpose, e.g. Class Car would contain code specifying what a Car can do and what it can look like. Thanks to a Class we can create an Object.

An Object is an instance of a Class. An object or an instance of class Car could be $myCar. And $myCar might be an old Mini Cooper with white roof and not so very impressive maximum speed. $yourCar, on the other hand, might be a stunning Jaguar, that my Mini could only dream of being. $myCar and $yourCar are two objects of class Car. They have very different characteristics, not only colour and maximum speed, but also number of passengers, engine size, horse power, you know. All of that can be specified within a class. An instance uses class specified characteristics and gives them its own values. As with cars, every object can also do different things. That’s where Methods make an entry.

You can see Methods as functions. Actually you define Methods as functions. Method is just the word you use when refering to a function within a class. The example class Car could have methods like startTheEngine(), showFuelLevel() or whatever. With methods you make your objects do things.

The OOP syntax

It is a very good practise to create a separate file for each of your classes. If you create class Car save it in a file car.php. To start your class simply type:

class Car {
// your code

Not very advanced. Within the {} brackets you write all the code of your class, variables and methods. You declare methods the same way as you do functions:

function showFuelLevel() {
// your code

The variables are not harder:


Going one step further

There are some other OOP specific things that might seem a bit tricky to start with. Those I would like to talk about here are:

  • scope
  • constructor and destructor
  • the $this-> word

With the scope you can specify access to a variable or method. Three most important ones are:

  1. private
  2. protected
  3. public

Private methods and variables are accessible only within the class, protected can be accessed by subclasses and public have no restrictions. The private and public scope is easy enough, the protected one introduces something called inheritance which is beyond the scope of this post.

Constructor is a special method within OOP which runs automatically when a new object (instance) is created. Sometimes you might want to have complete control over how objects are created. Perhaps you want to initiate some variables every time a new object is created. That’s what constructor is for. Destructor’s purpose is to destroy an object, use it if you want to have absolute control over how objects are destroyed.

All methods, including constructors, can receive parameters. If your constructor awaits parameters you need to send them when you create a new object. Within the class you type:

function __construct($param1, $param2) {
// some code

Notice the double underscore at the beginning of the constructors name. The name is reserved for constructor only!

In the file implementing the class Car you would type:

$myCar = new Car($param1, $param2);

The two parameters you pass on to the new object (to class Car’s constructor) might be some variables you have in your implementation file: a string, integer or maybe an array .

The $this-> word is a thing you just have to learn to use. You use it only within the class file to access variables and methods specified in the class. For example, in our Car class you could have the following code:

private $maxSpeed;

function setMaxSpeed($speed) {
$this->maxSpeed = $speed; /* assigns the value of the parameter to the maxSpeed variable */

To call a method within your class you would write:

function someFunc() {

The  -> operator is also used outside the class file as well. If you’d like to set maximum speed of your Jaguar you would do it the following way:

$myCar->setMaxSpeed(’300 mph’);

That code would be somewhere in the implementation file after you have created an object of class Car. Once you have created an object, you can start using the methods included in your class.


This was quite an exhaustive introduction to OOP, PHP or not. I hope it doesn’t feel overwhelming. If it does, please do ask questions in the comments below and I might write a new post if a simplification is needed. If questions will be those of hungry for more OO PHP I will, as the plan is, publish more posts on the subject. Can’t promise yet when, but a tutorial is coming on how to create your own class for managing forms including validation and database submission.