OOPS Features Supported by PHP

PHP supports both procedural and Object Oriented programming. Object Oriented methodologies will be useful and effective while working with complex or huge size projects. It helps to compartmentalize the project based domains and programming layers to make it easy to handle. In this tutorial, we are going to see some of the important OOPS features supported by PHP like Class, Object, Encapsulation, Inheritance and more. I have described these features with simple examples.

animal-classification

Class

The class is a programming block structure contains its own properties and methods to process the domain functionalities. We can create instances to a class to access its properties and method. The following example code shows a Mobile() class containing the properties and methods. In this example, I have specified access specifier for the class properties and methods. These access specifiers are used to restrict the visibility of the class properties and methods.

class Mobile {
   private $premiumModal = "iPhone";
   public function getPrimiumModal() {
      return $this->premiumModal;
   } 
}

Object

Objects or Instances can be created for a class to access the class components. The below PHP code shows how to create an object to the Mobile() class and it is used to access the class components.

class Mobile {
   private $premiumModal = "iPhone";
   public function getPrimiumModal() {
      return $this->premiumModal;
   } 
}
$object = new Mobile();
echo "<b>Premium Modal:</b> " . $object->getPrimiumModal() . "<br/>";

Constructor and Destructor

The Constructor is a class function called automatically when an instance is created for that class. The Destructor is a function that is invoked when the class instance is destroyed. In PHP, the constructor and the destructor are defined by using the magic functions __construct() and __destruct() respectively. The following code shows an example with the class constructor used to initialize its private property.

<?php
class Mobile {
	private $modal;
	function __construct() {
	    $this->modal = array("iPhone","Samsung Galaxy", "MotoG");
	}
	function printAllModal() {
	    foreach($this->modal as $modal) {
	    	print $modal . "<br/>";
	    }
	} 
}

$object = new Mobile();
echo "<b>All Modals:</b><br/>";
$object->printAllModal();
?>

Inheritance

There are three types of inheritance like single, multiple and multi-level inheritance. PHP supports single and multi-level inheritance. It will not support multiple inheritance. That is, A class can extend only one parent class. To overcome this limitation, PHP provides the Traits concept which allows us to use functions of one class into one or more classes. We have detailed article about PHP inheritance with more examples.

Encapsulation

Since properties and methods of the objects are enclosed with the class container in an 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.

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

↑ Back to Top