OOPS Features Supported by PHP

In PHP, it supports object oriented features which lead further popularity to this language. PHP language focus on the following OOPS features.

  • Class
  • Object
  • Encapsulation
  • Inheritance

After discussing these features, we can gain knowledge about how to retrieve required data from the object data structure that is defined by the class.

Class

Like other object oriented languages, PHP allow user to define their own class with it’s appropriate properties and methods. For example, a simple class is defined as follows.

class Controller {
public $action;
public $method;
function formatCurrentDate() {
$currentDate = date('d-m-Y');
return $currentDate;
}
}

The class Controller is defined with two properties and one method. Let us discuss, how to access these things using PHP program.

php_object

Object

Objects are used to access the data that are in the class corresponding which objects are created. In PHP objects are created as follows.

Controller $objController = new Controller();

Thus the instance of Controller class is created. Using this instance, the properties and functions can be accessed with -> operator. For example,

$objController->$action;
$objController->$method;
$date = $objController->formatCurrentDate();

Encaptulation

Since, properties and methods of the objects are enclosed with the class container in abstract manner, any code from outside can not access the class, unless otherwise by using objects of that class. In this way, the properties are encapsulated.

Inheritance

PHP supports only single inheritance. That is, only one subclass can be derived from the parent class. We can not use the methods of a particular class in more than one independent class using this inheritance concept. To fill this gap, there is a feature in PHP called as Traits which allow us to use functions of one class into one or more independent classes.

This PHP code tutorial was published on April 24, 2013.

↑ Back to Top