We must have heard about object-oriented programming in C++ and Java; similarly, PHP also supports Object-Oriented Programming (OOP), where we deal with classes and objects to create a program. Here classes are the data structures, and objects are the blueprints that represent these data structures. This tutorial briefly explains PHP Object-Oriented Programming terminology with an example.

PHP OOP Terminology

Before going into detail about PHP OOP, it is necessary to know its essential terms:

Keyword Description
Class
  • Class is one kind of programmer-defined data type.
  • It is a collection of objects, also called a blueprint.
  • It comprises local methods as well as a local variable.
  • We use the keyword ‘class’ to create one.
Object An object is an instance of a class.
Member Variable Member variables are defined inside the class, and only member functions can access them.
MemberFunction Member functions are operators and functions declared as members of a class and used for accessing object data.
Inheritance Inheritance is a concept where a child class can inherit the functions of a parent class.
Parent class A class is said to be a parent if another class inherits its member functions. It is also called a base class.
Child class A class is said to be a child class if it inherits member functions from another class. It is also called the derived class.
Polymorphism In OOP, Polymorphism means many forms. Generally, polymorphism occurs if there is a hierarchy of classes and the law of inheritance relates to them.
Overloading Overloading can be achieved if some class methods have a similar name but different parameters.
Data Abstraction In data abstraction, some implementation details remain hidden.
Encapsulation Encapsulation is a way to encapsulate all the data and member functions together as one object.
Constructor A constructor is a special function that allows the object’s properties to be initialized automatically upon the creation of the object.
Destructor A destructor is a function that gets called automatically when the object is destroyed, or the script execution is completed or exited. Example

PHP Class

We can define a class by using the keyword ‘class’ followed by the name of that class:

An Example of a PHP Class:

<?php
/*Example Class*/
class Example
{
  /* Member variables */
  public $Name, $Age;

  /*Constructor method*/
  public function __construct($UserName, $UserAge)
  {
    $this->Name = $UserName;
    $this->Age = $UserAge;
  }

  /*Destructor method*/
  public function __destruct(){
    echo 'The class "' . __CLASS__ . '" was destroyed!';
  }
}

/*Create the class object*/
$Obj1 = new Example("Tom", 22);
$Obj2 = new Example("Dick", 26);
$Obj3 = new Example("Harry", 28);

print_r([$Obj1, $Obj2, $Obj3]);
?>
Output:
Array
(
    [0] => Example Object
        (
            [Name] => Tom
            [Age] => 22
        )

    [1] => Example Object
        (
            [Name] => Dick
            [Age] => 26
        )

    [2] => Example Object
        (
            [Name] => Harry
            [Age] => 28
        )

)

The class "Example" was destroyed!
The class "Example" was destroyed!
The class "Example" was destroyed!
The PHP’ $this’ is a reserved keyword that refers to the existing class object.