Chapter 3-Apply Object Oriented Concepts in PHP
Chapter 3-Apply Object Oriented Concepts in PHP
•Default Constructor
•Parameterized Constructor
Default Constructor
•A default constructor is a constructor that has
no parameters.
•Example:
class Student
{
function __construct()
{
echo “Default constructor”;
}
}
$stud=new Student;
Parameterized Constructor
•The constructor that can take the arguments are called parameterized
constructors.
•Example:
class Student
{
public $roll;
function __construct($enroll)
{
$this->roll=$enroll;
echo $this->roll;
}
}
$stud=new Student(10);
Note: Once you create your own constructor, the default constructor is
no longer accessible.
Destructors
•In PHP destructor are called when you explicitly
destroy an object or when all references to the
object go out of scope.
•Destructor have a special name in PHP __destruct.
•Destructor do not have any arguments.
•Syntax :
function __destruct()
{
echo “Destroyed”;
}
Example:
<?php
class Student
{
public $roll;
function __construct($enroll)
{
$this->roll=$enroll;
echo $this->roll;
}
function __destruct()
{
echo “<br>object trashed”;
}
}
$stud=new Student(10);
?>
Output :
10
object trashed
Inheritance:
●The mechanism of deriving a new class from an old
one is called inheritance or derivation.
Old Class
New Class
Super Class and Sub Class:
−The old class is referred to as the Super class and
the new one is called the Sub class.
●Parent class : Base class or super class
●Child Class : Derived class, Extended class or sub
class.
Type of Inheritance :
●Single Inheritance
●Multiple 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 parent_class_name
{
members of class
}
class child_class_name extends Parent_class_name
{
members of class
}
Example:
<?php
class father
{
public $a,$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 :
Father
Son
GrandSon
Syntax :
class father
{
Members of class
}
class son extends father
{
Members of class
}
class grandson extends son
{
Members of class
}
Multiple Inheritance :
C
Example :
class C extends A
<?php {
class A use B;
{ function disp2()
function disp() {
{ echo “Child-C”;
echo “Parent-A”; }
} }
} $obj=new C();
trait B $obj->disp();
{ $obj->disp1();
function disp1() $obj->disp2();
{ ?>
echo “Parent-B”;
}
}
Output:
Parent-A
Parent-B
Child-C
Hierarchical Inheritance
Father
Child
Method Overloading :
?>
Method overriding :
function __construct($name,$rollno)
{
$this->name=$name;
$this->rollno=$rollno;
}
/*function __clone()
{
echo "<h3>Copying object.....</h3>";
}
*/
function display()
{
echo"<h4> Name:$this->name</h4>";
echo"<h4> RollNo:$this->rollno</h4>";
}}
$s1=new student("Ashwini",101);
$s2=new student("Sharda",102);
$s1->display();
//$s2=clone $s1;
$s2->display();
?>
<?php
class parent1
{
public function myfun()
{
echo “<br>***** In parent class ****”;
echo “<br>This function is inside:”.get_class($this);
}
}
class child extends parent1
{
public function myfun()
{
echo “<br>***** In child class ****”;
echo “<br>This function is inside:”.get_class($this);
echo “<br>This parent of this class is:”.get_parent_class($this);
}
}
if(class_exists(“parent1”))
{
$obj_p1=new parent1();
echo “<br> the class name is :”.get_class($obj_p1);
$obj_p1->myfun();
}
if(class_exists(“child”))
{
$obj_c=new child();
echo “<br> the class name is :”.get_class($obj_c);
$obj_c->myfun();
}
?>
Output :
the class name is :parent1
***** In parent class ****
This function is inside:parent1
unserialize() can use this string to recreate the original variable values.
Example :
<?php
$a=array("a"=>"Rupali","b"=>"Sonali");
print "Serialize array";
$str=serialize($a);
print $str;
echo "<br>";
print "Unserialize array";
$arr=unserialize($str);
print_r($arr);
?>
Output :
Serialize arraya:2:{s:1:"a";s:6:"Rupali";s:1:"b";s:6:"Sonali";}
Unserialize arrayArray(
[a] => Rupali
[b] => Sonali
)