October 29, 2026

PHP – Object Oriented Programming – 1

In this chapter, we would cover Object Oriented Programming(OOP) concepts in PHP with examples.

Object Oriented Programming is a way to write programs in terms of objects in the real world. So just like the world around us is made up of objects, we would also have our program made up of objects. And then these objects would be have some attributes and also be able to interact in some way.

  1. Object Oriented Programming Concepts
  2. Advanced Object Oriented Programming Concepts
  3. Defining a Class

 

1. Object Oriented Programming Concepts

Before we would consider some examples, let’s first take a look at some OOP concepts.

Object – an object is simply what it is. Something, that has some attributes and behavior. For instance Vehicle(has wheels and speed), animal etc. Such objects can be defined in OOP as we would see later

Class – this is a blueprint for creating objects. A class is created in PHP as a programmer-defined data type. I would specify what the attributes(member variables) and behaviour(member functions) of it’s objects would be. So once you have a class, you can then use that class to create object by a process called instantiation. Therefore, we say an objects is an instance of a a class

Member Variable – these a variable defined in a class as attributes of the class. They are normally invisible outside the class and therefore accessible using the member functions

Member Functions – these are functions defined inside a class. They specify what operations the class can perform. They are also used to access the class member variables.

Inheritance – this is  a way for a class to be created based on existing class. It then can inherit some member variables or functions from the parent class

Parent Class – This is a class whose variables or functions are inherited by another class. It is also called parent class, super class or base class

Child Class – this is a class that inherits from another class. It is also known as derived class or subclass

 

2. Advanced OOP Concepts

Other advanced object oriented programming concepts are outlined below:

Polymorphism – this is a situation where a the same function can be used to perform different operations. For instance, two function may  have the same name but the operation to perform would be based on the type and number of it’s parameters

Overloading – this is a type of polymorphism where things like operators or functions can be made to have different implementations

Abstraction – also data abstraction. This is  a way to keep certain implementation details hidden from the users. It is related with Information Hiding.

Encapsulation – this is a concept of bundling(encapsulating) all the data and functions of an object together in a class.

Constructor – this is a special type of function that has the name of the class and is invoked automatically whenever the  an object of the class is instantiated

Destructor – this is a special type of function that is called when objects goes out of scope.

 

3. Defining a Class

A class in PHP is defined using the class keyword, based on the following syntax:

<?php
    class className {
        var $memberVariable1;
        var $memberVariable2 = "I am member variable";

        function memberFunction($param1, $param2){
            /*
            function body
            */
        }
        //more codes
    }
?>

Take note of what happens in the code:

  • First you use the keyword class, followed by the name of the class
  • The content of the class must be in curly braces
  • Member variables are defined using the keyword var
  • Member variables defined can also be assigned values at the point they are defined
  • Functions definition begin with the keyword function, and just exactly like functions are defined as we covered under PHP Functions

Let’s take an example. Here, we define a class called Product

<?php
    class Product {
        //member variables
        var $name;
        var $description;
        var $price;
        var $stock;

        //member function
        function addStock($quantity){
            $this->stock = $stock + $quantity;
        }

        function getPrice() {
            return $this->price;
        }

        function setPrice($newPrice){
            $this->price = $newPrice;
        }
    }
?>

The $this variable is a special PHP variable and it refers to the current object.

As an exercise for you, create classes for the following object: Car, Animal and Oven. Each of the classes should have two member variables and two member functions.

In the next part, we would further explore Object Oriented Programming in PHP and see how we can create objects from classes.