Unit -3 Apply Object Oriented Concepts in PHP
Unit -3 Apply Object Oriented Concepts in PHP
// destructor
function __destruct()
{
// clearing the object reference
}
}
?>
Single Inheritance
In a single inheritance, there is only one base class and one sub or derived class. It
directly inherits the subclass from the base class.
<?php
//PHP program to demonstrate the single inheritance.
class Base
{
function BaseFun()
{
echo "BaseFun() called<br>";
}
}
class Derived extends Base
{
function DerivedFun()
{
echo "DerivedFun() called<br>";
}
}
$dObj = new Derived();
$dObj->BaseFun();
$dObj->DerivedFun();
?>
Output
BaseFun() called
DerivedFun() called
Multi-Level Inheritance
In the multi-level inheritance, we will inherit the one base class into a derived class, and
then the derived class will become the base class for another derived class. <?php
//PHP program to demonstrate the multi-level inheritance.
class Base
{
function BaseFun()
{
echo "BaseFun() called<br>";
}
}
class Derived1 extends Base
{
function Derived1Fun()
{
echo "Derived1Fun() called<br>";
}
}
class Derived2 extends Derived1
{
function Derived2Fun()
{
echo "Derived2Fun() called<br>";
}
}
$dObj = new Derived2();
$dObj->BaseFun();
$dObj->Derived1Fun();
$dObj->Derived2Fun();
?>
Output
BaseFun() called
Derived1Fun() called
Derived2Fun() called
Hierarchical Inheritance
In the hierarchical inheritance, we will inherit the one base class into multiple
derived classes.
<?php
//PHP program to demonstrate the hierarchical inheritance.
class Base
{
function BaseFun()
{
echo "BaseFun() called<br>";
}
}
$Obj1->BaseFun();
$Obj1->Derived1Fun();
echo "<br>";
$Obj2->BaseFun();
$Obj2->Derived2Fun();
Output
BaseFun() called
?> Derived1Fun() called
BaseFun() called
Derived2Fun() called
Multiple Inheritance(Using interface)
Multiple inheritance is a type of inheritance in which one class can inherit the
properties from more than one parent class.
PHP doesn’t support multiple inheritance but by using Interfaces in PHP ,we can
implement it.
<?php
//PHP program to implement multiple-inheritance using the
interface.
class Base
{
public function Fun1()
{
printf("Fun1() called<br>");
}
}
interface Inf
{
public function Fun2();
}
class Derived extends Base implements Inf
{
function Fun2()
{
printf("Fun2() called<br>");
}
function Fun3()
{
printf("Fun3() called<br>");
}
}
$obj->Fun1();
$obj->Fun2();
$obj->Fun3();
Output
?> Fun1() called
Fun2() called
Fun3() called
<?php
class A {
public function insideA()
{
echo "I am in class A";
}
}
interface B
{
public function insideB();
}
class C extends A implements B
{
function insideB()
{
echo "I am in interface";
}
public function insideC()
{ Output:
echo "I am in inherited class"; I am in class A
I am in interface
} I am in inherited class
}
$obj = new C();
$obj ->insideA();
$obj ->insideB();
$obj ->insideC();
?>
Method Overloading
Function overloading or method overloading is the ability to create multiple functions of
the same name with different implementations depending on the type of their arguments.
In PHP overloading means the behavior of a method changes dynamically according to
the input parameter.
There are two types of overloading in PHP.
1. Property Overloading
2. Method Overloading
PHP supports method overloading using a magic keyword, __call.
__call keyword
This is a magic method that PHP calls when it tries to execute a method of a class and it
doesn't find it.
This magic keyword takes in two arguments:
function name
arguments to be passed into the function.
Its definition looks like this:
function __call(string $function_name, array $arguments)
{
}
Example
Let's understand method overloading with an example.
<?php
class Shape
{
const PI = 3.142 ;
function __call($name,$arg)
{
$x=count($arg);
if($name == 'area')
switch($x))
{
case 0 : return 0 ;
case 1 : return PI * $arg[0] ;
case 2 : return $arg[0] * $arg[1];
}
}
}
$circle = new Shape();
Output:
echo $circle->area(3); 9.426
$rect = new Shape(); 48
<?php
class CloneDemo
{
var $msg = "This is an example of cloning Object";
}
$a = new CloneDemo;
$b = clone $a;
$a->msg= "Assigned new resource";
echo "Original: " . $a->msg;
echo "Cloned: " . $b->msg;
?>
Program to create copy of an object.
Introspection in PHP:
Introspection in PHP offers the useful ability to examine an object's
characteristics, such as its name, parent class (if any) properties, classes,
interfaces, and methods, and functions at runtime
PHP offers a large number of functions that you can use to accomplish the
task.
The following are the functions to extract basic information about classes
such as their name, the name of their parent class and so on.
In-built functions in PHP Introspection :
Serialization in PHP:
Serialization is a technique used by programmers to preserve their working
data in a format that can later be restored to its previous form.
Serializing an object means converting it to a byte stream representation
that can be stored in a file.
Serialization in PHP is mostly automatic, it requires little extra work from you,
beyond calling the serialize() and unserialize() functions.
Serialize():
The serialize() converts a storable representation of a value.
The serialize() function accepts a single parameter which is the data we
want to serialize and returns a serialized string
A serialize data means a sequence of bits so that it can be stored in a file, a
memory buffer, or transmitted across a network connection link. It is useful
for storing or passing PHP values around without losing their type and
structure.
Syntax:
serialize(value);
unserialize():
unserialize() can use string to recreate the original variable values i.e.
converts actual data from serialized data.
Syntax:
unserialize(string);
Example:
<?php
$a=array('Shivam','Rahul','Vilas');
$s=serialize($a);
print_r($s);
$s1=unserialize($s);
echo "<br>";
print_r($s1);
?>
Output:
a:3:{i:0;s:6:"Shivam";i:1;s:5:"Rahul";i:2;s:5:"Vilas";}
Array ( [0] => Shivam [1] => Rahul [2] => Vilas )