Object Oriented Programming Principles
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
Encapsulation:
PHP means, enclosing the internal details of the object to protect from external sources. It describes, combining the class, data variables and member functions that work on data together within a single unit to form an object.
PHP program for encapsulation: The methods or functions in the following program are update password and check the course name. user class defines all the operations related to users.
<?php
// PHP program to implements encapsulation
class user {
private $userId;
private $pwd;
// Update user password
public function updatePwd($userId, $pwd) {
// Write function body
echo("Function to update password '"
. $pwd . "' for user " . $userId);
echo "<br>";
}
// Check account balance
public function courseName($userId) {
// Write function body
echo("Function to check course name of user "
. $userId);
echo "<br>";
}
}
$obj = new user();
$obj -> updatePwd('user1', 'abhi2');
$obj -> courseName('PHP');
?>
Output:
Function to update password ‘abhi2’ for user user1
Function to check course name of user PHP
Advantage of Encapsulation :
- It reduces human errors.
- It makes easier to understand.
- It’s protects an object from unwanted access by clients.
- Encapsulation reduces human errors.
Inheritance:
- Accessing the features of one class from another class.
- Inheritance allows you to write the code in the parent class and use it in both parent and child classes.
- The parent class is also called a base class or super class. And the child class is also known as a derived class or a subclass.
Declaration of Child Class :
class ChildClassName extends ParentClassName
{
members of Child class
};
Type of Inheritance in php:
- Single Inheritance
- Multi-level Inheritance
- Hierarchical Inheritance
Single Inheritance:
If a class is derived from one base class (Parent Class), it is called Single Inheritance.
Syntax:
class Father
{
members of class Father
};
class Son extends Father
{
members of class Son
};
Example of single level Inheritance:
<?php
class Father
{
public $a;
public $b;
function getvalue($x, $y)
{
$this->a = $x;
$this->b = $y;
}
}
class son extends Father{
function display()
{
echo"value of A:$this->a <br>";
echo"value of B:$this->b <br>";
}
}
$obj = new son;
$obj->getvalue(10,20);
$obj->display();
?>
Multi-level Inheritance:
In multi-level inheritance, the class inherits the feature of another derived class (Child Class).
syntax:
class Father
{
members of class Father
};
class Son extends Father
{
members of class Son
};
class GrandSon extends Son
{
members of class GrandSon
};
Example of Multi-level Inheritance:
<?php
class Father
{
public $a;
public $b;
function getvalue($x, $y)
{
$this->a = $x;
$this->b = $y;
}
}
class son extends Father
{
public $c=30;
public $sum;
function add()
{
$this->sum=$this->a+$this->b+$this->c;
return $this->sum;
}
}
class grandson extends son
{
function display()
{
echo "value of A :$this->a <br>";
echo "value of B :$this->b <br>";
echo "value of C :$this->c <br>";
echo "value of sum : $this->sum <br>";
}
}
$obj = new grandson;
$obj->getValue(10,20);
$obj->add();
$obj->display();
?>
Hierarchical Inheritance:
class Father
{
members of class Father
};
class Son extends Father
{
members of class Son
};
class Daughter extends Father
{
members of class Daughter
};
Example of Hierarchical Inheritance:
<?php
class Father
{
public $a;
public $b;
function getvalue($x, $y)
{
$this->a = $x;
$this->b = $y;
}
}
class son extends Father
{
public $c=30;
public $sum;
function add()
{
$this->sum=$this->a+$this->b+$this->c;
return $this->sum;
}
function display(){
echo "value of A:$this->a <br>";
echo "value of b:$this->b <br>";
echo "value of c:$this->c <br>";
echo "sum: $this->sum <br>";
}
}
class Daugther extends Father
{
public $c=30;
public $multi;
function multiply()
{
$this->multi=$this->a*$this->b*$this->c;
return $this->multi;
}
function display(){
echo "value of A:$this->a <br>";
echo "value of b:$this->b <br>";
echo "value of c:$this->c <br>";
echo "multipication: $this->multi";
}
}
$objs = new son;
$objD = new Daugther;
$objs->getValue(10,20);
$objs->add();
$objs->display();
$objD->getValue(10,20);
$objD->multiply();
$objD->display();
?>
polymorphism:
Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance
Real life example of polymorphism: A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person possesses different behavior in different situations. This is called polymorphism.
Example of polymorphism:
<?php
class base
{
function add($a,$b)
{
$res=$a*$b;
echo "Multiplication = ".$res;
}
}
class child extends base
{
function add($a,$b)
{
$res=$a+$b;
echo "Sum = ".$res;
}
}
$obj= new child();
$obj->add(1000,500);
?>
Output:
Sum = 1500