Introduction : –
OOPs( Object Oriented Programming) is a core concept of PHP. Object means a data which is structured through a class. In Object Oriented Programming the object is created i.e., the data and code are written and the properties and values in the object are defined in a way. A class can have many objects and all those objects are instances of the class and they have variables, methods, properties and values. OOPs concept is a concept of many languages.
Note :- Object Oriented Programming is one of the main concepts of PHP, which is very important to know and understand, if you want to learn PHP.
Visit this link https://easywebprogramming.com/what-is-php-programming if you know about PHP in detail, the article given in this link gives detailed information about PHP Language.
Top Features Of OOPs: –
OOPs is a concept that exists in a large number of languages. Oops concept has many such features due to which many languages use this concept.
1. Object and Classes : – The main feature of the Oops concept are objects and classes. PHP is an object oriented language. PHP consists of objects and classes which increase programming enhancement performance and it makes programming very easy. Object Oriented means that in PHP the script is divided into parts so that the script step can be easily executed and it becomes easy to understand the script, so in PHP first class is created and object is defined.
What is Object ? : Object is a collections of methods, attribute and variables. Each object has attributes and methods according to which the script works and the output is obtained.
See the object example below : –
<?php class Fruit { function getFruit() { echo "Mango"; } } $fruits = new getFruit; $fruits->getFruit(); ?>What is Class ? : Class is a collection of objects. When scripting is start in PHP then a class is created or after that there is a class of functionality in which we define the behavioral data we need.
See the Class example below :
<?phpclass Fruit { // code here } ?>
2. Abstraction : – Abstraction features are one of the top features of OOPs concept. The abstract concept hides functionality and shows only as much information as is needed to the user. Abstraction is considered a core feature of the security concept.
See the abstraction example below :
<?php abstract class Fruit { public $name; public function Describe() { return $this->name . ", ". " is a fruit"; } abstract public function Greet(); } class apple extends Fruit { public function Greet() { return "Apple!"; } public function Describe() { return parent::Describe(); } } $fruit = new apple(); $fruit->name = "apple"; echo $fruit->Describe(); echo $fruit->Greet(); ?>
3. Encapsulation : – Encapsulation welds data and methods together. Inside a class there is an object, that object contains many variables, and data, anything that combines them is called encapsulation.
See the encapsulation example below :
<?php class Admin { private $admintid; private $adminEmail; public function changeEmail($admintid,$adminEmail) { //code here } } $obj = new Admin(); $obj ->ChangeEmail(1,'admin@gmail.com'); ?>
4. Inheritance : – Inheritance is the base of object oriented programming and Inheritance is one of the features of OOPs programming. When a child class is derived from a main class it is called inheritance.
See the inheritance example below :
<?php class fruit { // properties , methods } class mango extends fruit { // inherit class fruit properties } ?>
Types of inheritance : –
- Single Inheritance – PHP supports single inheritance. There are two classes in this inheritance, the base class and the child class. Child class derived from base.
- Multilevel Inheritance – PHP supports multilevel There are three classes in this inheritance, the base class and the two child classes. The class of the third child is inherited from the second child. The second class is considered as the base class for the third child class.
- Multiple Inheritance – PHP not supports Multiple In multiple inheritance, child classes can be derived from multiple base classes.
- Hierarchical Inheritance – PHP supports single inheritance. It follows a hierarchical pattern of inheritance. Inheritance consists of a base class and many child classes.
Other Main concepts of Object Oriented Programming : –
-
Polymorphism : – Polymorphism defines multiple forms and functions over patterns. Polymorphism handles how different classes respond in different ways to different forms.
See the polymorphism example below :
<?php class Fruit { function getFruits($apple,$mango) { $result = "Fruit is".$apple." and ".$mango; echo $result; } } class getFruits extends Fruit { function addfruit($apple,$mango, $grapes) { $result = "Fruit is".$apple." and ".$mango." and ".$grapes; echo $result; } } $obj= new addfruit(); $obj->add("Apple","Mango","Grapes"); ?>
-
Constructor : – Constructor is used for object initialization. Constructor is created in the class and the constructor object can be called inside the method created inside the class.
See the constructor example below:
<?php class Fruit { public $fruitName; function __construct($fruitName) { $this->fruitName = $fruitName; } function fruitName() { return $this->fruitName; } } $apple = new Fruit("Apple"); echo $apple->fruitName(); ?>
-
Destructor : – destructor function deallocates memory destruct is a member function that is called immediately after the object is destroyed.
See the destructor example below:
<?php class Fruit { public $FruitName; function __construct($FruitName) { $this->FruitName = $FruitName; } function __destruct() { echo "The fruit is {$this->FruitName}."; } } $apple = new Fruit("Apple"); ?>
-
Access Modefier : –
Access modifiers allow access to methods and properties according to public, private, or protected Access modifiers.
Public access modifier : this modifier using access everywhere of class.
See the public access modifier example below:
<?php class AdminDetail { public $adminName="admin"; function disp() { echo $this->adminName."<br/>"; } } class user extends AdminDetail { function show() { echo $this->adminName; } } $result= new user; echo $result->name."<br/>"; $result->disp(); $result->show(); ?>