OOPS Features Supported by PHP

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.

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 “Premium Modal: ” . $object->getPrimiumModal() . “
“;

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.
modal = array(“iPhone”,”Samsung Galaxy”, “MotoG”);
}
function printAllModal() {
foreach($this->modal as $modal) {
print $modal . “
“;
}
}
}

$object = new Mobile();
echo “All Modals:
“;
$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.

Previous:PHP LoopsNext:Dates and Time in PHP
↑ Back to Top

PHPPHP IntroductionPHP BasicsPHP Shopping CartPHP Contact FormTypes, Variables & OperatorsPHP StringsPHP ArraysPHP FunctionsPHP OOPSOOPS Features Supported by PHPPHP Constructors DestructorsPHP Access ModifiersPHP InheritancePHP InterfacesOverloading in PHPPHP Magic MethodsUse of PHP finalPHP self Vs thisPHP Object CloningPHP FormsAdvancedPHP AJAXRESTful APIPHP DatabasesPHP Sessions and CookiesError and Exception HandlingFiles and DirectoriesPHP Date TimePHP GraphicsPHP XMLPHP Code SamplesPHP FreelancerjQueryWordpressMySQLCSS

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons
Social Media Auto Publish Powered By : XYZScripts.com