Explanation of object-oriented programming concepts and features in PHP

Explanation of object-oriented programming concepts and features in PHP

PHP Programming (PHP: Hypertext Preprocessor) Logo

What is object-oriented programming?

Object-oriented programming is a programming concept that primarily involves the separation of a large portion of programming logic and arbitrary PHP code in individual files and presentation markup (HTML and CSS code). And with the features of object-oriented programming, it means application code is cleaner, easier, more organised and coherent and easier to develop, maintain and extend, applications coded with object-oriented programming.

In object-oriented programming, a class is the interface for object-oriented code (in other words, a wrapper) and the functions within the class (also known as methods) contain the application logic/functionality. A class also consists of class-level variables (which are also referred to as class properties). The context and use of classes, methods and properties likely may not make sense to you if you have never created any applications using object-oriented programming concepts, but with object-oriented programming, if you’re creating a very complex and large application, object-oriented programming can help a lot to keep application code and functionality coherently in place, easy for multiple developers to participate in one project and make it easier for application functionality to be extended in the future.

Features of object-oriented programming.Object Oriented Programming - Parent and Subclasses Image


Inheritance is where the logic and functionality of one class is inherited and used elsewhere in another part of your application with another class; to the left is an illustration of inheritance of this. As you can see, the Text, Shape, Graphics and System class is inherited in your Application class (in this example your application has a class called Application).

Your Windows application.

If you’re making a Windows application, you’ll likely be using Microsoft’s .NET application framework (which is fully object-oriented). Let’s use an abstractive example here where the classes on the left actually exist as they are in the .NET framework. How do you display text on your application interface? Well, you use the Text class. But what if you want to use the Text class but make some changes that aren’t available with the Text class by default? This is where inheritance becomes very useful. When you inherit the Text class, you can modify the methods in the Text class to suit your needs, and also call the methods of the Text class within any method of your Application class.

But by inheriting the Text Class, you also inherit the Shape class, Graphics class and the System class, in this case.

Class-level variables.

A class-level variable is simply a variable that is within a class and, depending on visibility scope, is available to its own class only, to base classes that are inheriting the class with the class-level variable or available to all classes inherited and otherwise, as well as available when the class is used in your application (when you create an instance of a class – as explained below).


Methods and class variables can be given three visibility scopes:

  • Public – this is where the method or class variable can be used and accessed anywhere possible – including when an instance of a class is created as well as in the case of classes being inherited (i.e. classes that are inheriting or a class being inherited).
  • Private – this is where the method or class variable can only be used or accessed from within the same class – classes that inherit classes cannot make use of private methods and private class variables. If you try and use methods or class variables that are declared as private outside of the same class (such as when the class is inherited by another class), it will result in a fatal error.
  • Protected – this is where the method or class variable can only be used and accessed from the same class and classes that are inheriting or a class being inherited. The difference is that when you create an instance of a class with protected methods and class variables, if you try and use methods and class variables declared was protected will result in a fatal error.

Static methods and class variables.

This simply means that a method or class variable can be used without having to first create an instance of the class where the method or class variable exists.

Creating an instance of a class.

In order to use the methods or class variables within a class (except in the case where methods or class variables are declared static), you have to create an instance of the class in question (also referred to as instantiation).

Constructor method.

The constructor method is the same as an ordinary class method and if included in your class, that method is executed upon instantiation without having to be called individually like every other method. Essentially, if you do not have a constructor method and you create an instance of a class, nothing will really happen because the individual method(s) have not been called.

Object Oriented Programming - Parent and Subclasses ImageAbstract classes.

Classes declared as abstract cannot be instantiated and must have at least one method also declared as abstract. Any method declared as abstract cannot have any programming logic in it; the programming logic must be defined by the class that inherits the abstract class. An abstract class may be useful if classes are going to be used by others and you don’t want the class to be directly instantiated in your application. Instead, the class is inherited by other classes. You may also wonder why you would want to use abstract classes. Consider the illustration to the right that was showed previously. Let’s say the Graphics class is actually an abstract class. It will likely have methods that are also declared as abstract and so would be nothing more than a container. This means any classes that inherit the Graphics class implements the abstractive method with programming logic in its own method (so perhaps the Shape class). However, you may assume why would it need to be abstract in the first place. Why not just create the method in the Shape class directly and forget using an abstract class and method in the first place? Well, you can do this, but if every subclass (i.e. the Shape class in this instance) needs to have functionality that is described in the abstractive “container” method of the Graphics class, then  if it is described as abstract, subclasses need to implement the abstractive method. In fact, an error will be returned by PHP if subclasses do not implement the abstract methods if they inherited an abstract class.

By “implement” I am referring to the subclass containing the programming logic/code for the abstract method.

So abstract classes are indeed useful in many scenarios, especially large applications.

Further resources on object-oriented programming.

We have many more tutorials on object-oriented programming including on our forum. Here’s some tutorials from us you may wish to look at:

Ben Stones



Leave your comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.