PHP OOPs: Object-Oriented Programming Concepts in PHP
What is Object-oriented Programming?
Object-Oriented Programming is a procedure for software development that is related to the concept of Class, Objects, Inheritance, and many other concepts. PHP OOPs is formulated in that way; it focuses on an object while programming. An object can be anything like employees, cars, bank accounts, etc.
A class describes the properties and methods of an object. The object-oriented programming procedure is close to real life, as we always deal with an object, execute operations on the object, and also use its methods and variables.
OOPs Concept in PHP
The OOPs concepts of PHP are given below:
· Class
· Object
· Inheritance
· Abstraction
· Encapsulation
· Polymorphism
Class: Class is a blueprint for any functional object, and it is defined by the programmer. It contains local methods as well as local variables. A class is defined in the program by using the class keyword, and followed by the name of the class and uses curly braces ({}).
For example: Human Being is a class that having body parts and performing many actions.
There are some rules for creating a class:
o The class name cannot include spaces.
o The class name should be started with a letter.
o The class name cannot be a reserved word of PHP.
Syntax of defining a class:
<?php
class Computer
{
// statements to be executed
}
?>
Example of Class in PHP:
<?php
class Computer
{
public function name(){
echo “<br>Laptop”;
}
public function price()
{
echo “<br>25000 Rs/-”;
}
}
//To create php object we have to use new operator as used in below
$obj = new Computer();
$obj->name();
$obj->price();
?>
Output:
Laptop
25000 Rs/-
Object: Classes cannot do anything without objects. We have to define a class once and then can make multiple objects from a class, and it is created by using the new keyword. It is also known as an instance. Each object contains all the properties and methods which are defined at the time of creating the class, but they will contain different property values.
Example of Object in PHP:
<!DOCTYPE html>
<html>
<body>
<?php
class Computer {
// Class Properties
public $name;
public $color;
// Class Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$acer = new Computer(); // object
$dell = new Computer(); // object
$acer->set_name(‘Acer’);
$dell->set_name(‘Dell’);
echo $acer->get_name();
echo “<br>”;
echo $dell->get_name();
?>
</body>
</html>
Output:
Acer
Dell
Inheritance: One of the main advantages of object-oriented programming is that it provides the inheritance method. Inheritance is defined as when a class derives the properties and methods of another class is known as inheritance. It allows us to create a reusable piece of code that is written once in the parent class but can be used multiple times as required in the child classes. A child class is defined by using the extends keyword.
Parent class: A class from which another class uses properties and methods is known as parent class. It is also known as base or super-class.
Child class: A class that uses the properties of the parent class is known as child class. It is also called a subclass or derived class.
Syntax:
class Parent
{
// Code of parent class
}
class Child extends Parent
{
// Code of child class
}
Types of Inheritance:
Generally, there are three kinds of inheritance single, multiple, and multi-level inheritance, but PHP supports single inheritance and multi-level inheritance. Even it does not support multiple inheritance, but we can implement it with the help of PHP interfaces.
- Single Level Inheritance: In Single Level Inheritance the child class extends the methods of Parent.
Example of Single level Inheritance in PHP:
<?php
class A
{
public function printItem($string)
{
echo ‘ <br>Hello : ‘ . $string;
}
public function printPHP() {
echo ‘<br>I am from Delhi’ . PHP_EOL;
}
}
class B extends A {
public function printItem($string) {
echo ‘<br>Hello: ‘ . $string . PHP_EOL;
}
public function printPHP() {
echo “<br>I am from Mumbai”;
}
}
$a = new A();
$b = new B();
$a->printItem(‘Rohit’);
$a->printPHP();
$b->printItem(‘Ravi’);
$b->printPHP();
?>
Output:
2. Multi-level Inheritance: In Multi-level inheritance, the parent class will be extended by the child class, and again another subclass will use the properties of a child class. As shown in the below figure:
Example of Multi-level Inheritance in PHP:
<?php
class A // parent class
{
public function myName()
{
return ‘ Name is BrownHecat’;
}
}
class B extends A // class B is child class
{
public function myFriendName()
{
return ‘ Name is BrownCat’;
}
}
class C extends B // Class C is another child (subclass) class
{
public function myOtherFrName()
{
return ‘Nmae is RobinHudd’;
}
public function myHistory()
{
echo “<br>Class A “ .$this->myName();
echo “<br>Class B “.$this-> myFriendName();
echo “<br>Class C “ . $this->myOtherFrName ();
}
}
$obj = new C();
$obj->myHistory();
?>
Output:
Abstraction: An abstract class is defined as it includes at least one abstract method. An abstract method is declared, but it is not implemented in the code. The abstract classes are used when we want to commit the programmer to define a specific class method, but we are sure only about the method name and not aware about the details of how it should be defined. The abstract keyword defines an abstract class.
Syntax:
<?php
abstract class base
{
abstract function printdata(); // Abstract function
function pr() // Not Abstract function
{
echo “Base class”;
}
}
?>
Example of Abstraction in PHP:
<?php
abstract class Laptops
{
public abstract function getCompanyName();
public abstract function getPrice();
}
class Swift3 extends Laptops
{
public function getCompanyName()
{
return “Acer” . ‘<br/>’;
}
public function getPrice() {
return 36000 . ‘<br/>’;
}
}
class ideapad extends Laptops {
public function getCompanyName() {
return “Lenovo” . ‘<br/>’;
}
public function getPrice() {
return 23000 . ‘<br/>’;
}
}
$Laptop = new Swift3();
$Laptop1 = new ideapad();
echo $Laptop->getCompanyName();
echo $Laptop->getPrice();
echo $Laptop1->getCompanyName();
echo $Laptop1->getPrice();
?>
Output:
Encapsulation: Today’s technical world, it is necessary to maintain the privacy of our important data. When the data is modified in one function that affects the other functions, so it causes a lot of problems in any software. To overcome this problem, PHP uses the concept of encapsulation.
Encapsulation is a concept of wrapping up internal details of the object to protect from external sources. It includes class, data variables, and member functions that work together on data within a single unit in the form of an object. Furthermore, in encapsulation, the data is not accessed directly; it is accessible with the help of functions that are written inside the class.
Example of Encapsulation in PHP:
<?php
class student
{
public $name;
public $Id;
function __construct($n, $i)
{
$this->name=$n;
$this->Id=$i;
}
public function setId($rol)
{
$this->rol=$rol;
}
public function display()
{
echo “welcome “.$this->name.”<br/>”;
return $this->Id-$this->rol;
}
}
$student=new student(“Sonyou”, 170);
$student->setId(20);
echo “Your id is “.$student->display().” and Topic is NLP”;
?>
Output:
Polymorphism: Polymorphism has the ability to take more than one form. It is an OOP pattern that allows various classes with different functionalities to share a common Interface. The benefit of polymorphism is that the code written in different classes doesn’t affects other classes, which belongs to it.
The polymorphism has two types:
1) Compile-time polymorphism
2) Run time polymorphism
PHP supports only Run time polymorphism.
The Runtime polymorphism refers to as a decision is made at runtime or we can have many subtypes implements for a superclass.
Example of polymorphism in PHP:
<?php
class Data
{
function publish(){}
}
class Article extends Data
{
function publish()
{
print “Article has been published.</br>”;
}
}
class Newspaper extends Data
{
function publish()
{
print “Newspaper has been published.</br>”;
}
}
class Book extends Data
{
function publish()
{
print “Ellipse has been published.”;
}
}
$Val=array(2);
$Val[0]=new Article();
$Val[1]=new Newspaper();
$Val[2]=new Book();
for($i=0;$i❤;$i++)
{
$Val[$i]->publish();
}
?>
Output: