Unit-III Apply Object Oriented Concepts in PHP
Unit-III Apply Object Oriented Concepts in PHP
By: Bhojankar M. N. 1
Unit-III 16 Marks
By: Bhojankar M. N. 2
Defining PHP Class
Classes and objects are the two main aspects of object-
oriented programming.
A class is a self-contained, independent collection of
variables and functions which work together to perform
one or more specific tasks.
While objects are individual instances of a class.
A class acts as a template or blueprint from which lots of
individual objects can be created.
When individual objects are created, they inherit the same
generic properties and behavior
3
By: Bhojankar M. N.
Defining PHP Class
The general form for defining a class in PHP is as
follows:
<? php
class phpClass {
var $var1;
var $var2 = "constant string";
function myfunction ($arg1, $arg2) {
[..] [..]
}
}
?>
4
By: Bhojankar M. N.
Defining PHP Class
By: Bhojankar M. N. 5
Defining PHP Class
<?php
class Subject
{
// Properties
public $name;
// Methods
function set_name($name)
{
$this->name = $name;
}
By: Bhojankar M. N.
Defining PHP Class
function get_name()
{
return $this->name;
}
}
?>
By: Bhojankar M. N.
Creating Objects
Once you defined your class, then you can
create as many objects as you like of that class
type. Following is an example of how to create
object using new operator.
$php = new Subject();
By: Bhojankar M. N.
Calling member functions
After creating objects, we will able to call
member functions related to that object. One
member function will be able to process
member variable of related object only.
Example shows how to set name for the calling
member functions.
$php->set_name('PHP');
9
By: Bhojankar M. N.
Calling member functions
Now you call another member functions to get
the values set by in above example −
echo $php->get_name();
10
By: Bhojankar M. N.
Calling member functions
<?php classdemo.php
//PHP script to create a class & object
class Subject
{
// Properties
public $name;
// Methods
function set_name($name)
{
$this->name = $name;
}
11
By: Bhojankar M. N.
Calling member functions
function get_name()
{
return $this->name;
}
}
$php->set_name('PHP');
echo $php->get_name();
echo "<br>";
?>
12
By: Bhojankar M. N.
PHP Class
Output:
13
By: Bhojankar M. N.
Assignment
<?php
class Book
{
// Properties
public $name;
public $price;
// Methods
function set_name($name,$price)
{
$this->name=$name;
$this->price=$price;
}
14
By: Jadhav S. B.
Assignment
function get_name()
{
echo ' Book name: ',$this->name;
echo ' Book price: ',$this->price;
}
}
$sub1= new Book();
$sub2= new Book();
$sub3= new Book();
$sub1->set_name('PHP',233.44);
$sub2->set_name('Android',434.54);
$sub3->set_name('Python',656.45);
15
By: Jadhav S. B.
Assignment
echo $sub1->get_name();
echo "<br>";
echo $sub2->get_name();
echo "<br>";
echo $sub3->get_name();
?>
Output:
16
By: Bhojankar M. N.
Creating Constructor
A constructor allows you to initialize an object's
properties upon creation of the object.
If you create a __construct() function, PHP will
automatically call this function when you create
an object from a class.
Notice that the construct function starts with
two underscores (__)!
using a constructor saves us from calling the
set_name() method which reduces the amount
of code:
17
By: Bhojankar M. N.
Creating Constructor
__construct():
Syntax:
function __construct()
{
// initialize object & its properties by assigning
values
}
Eg:
function __construct()
{
echo “This is default constructor..”;
} 18
By: Bhojankar M. N.
Creating Constructor
Constructor types:
Default Constructor: It has no parameters,
but the values to the default constructor can be
passed dynamically.
By: Bhojankar M. N.
Calling Constructor
<?php classdemo2.php
class Book
{
public $name;
public $price;
20
By: Bhojankar M. N.
By: Jadhav S. B.
Calling Constructor
function get_name()
{
return $this->name;
}
function get_price()
{
return $this->price;
}
}
$php=new Book("PHP","434.54");
echo "Book name:",$php->get_name();
echo "<br>";
echo "Book price:",$php->get_price();
?> 21
By: Jadhav S. B.
User defined Constructor:
In the case of Pre-defined
Constructor(__construct) and user-defined
constructor in the same class, the Pre-defined
Constructor becomes Constructor while user-
defined constructor becomes the normal
method.
22
By: Bhojankar M. N.
Calling Constructor
<?php classdemo3.php
class Tree
{
function Tree() //User defined
constructor
{
echo "Its a User-defined Constructor
of class Tree";
}
function __construct() //Predefined
Constructor
{
echo "Its a Pre-defined Constructor of
the class Tree"; 23
By: Jadhav S. B.
}
PHP Constructor
Output:
24
By: Bhojankar M. N.
Creating Destructor
A destructor is called when the object is
destructed or the script is stopped or exited.
If you create a __destruct() function, PHP will
automatically call this function at the end of the
script.
It function starts with two underscores (__).
The example below has a __construct() function
that is automatically called when you create an
object from a class, and a __destruct() function
that is automatically called at the end of the
script:
25
By: Bhojankar M. N.
Calling Constructor &
Destructor
<?php // dest.php
class Fruit {
public $name;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}
$apple = new Fruit("Apple");
?>
26
By: Bhojankar M. N.
Inheritance in PHP
In object oriented programming, Inheritance
enables a class to use properties and methods
of an existing class.
Often while coding we come across situations
where in we have to create a new class with all
the functionalities of an existing class and some
additional methods.
It is an extension to an existing class, in such
cases, we can either copy all the properties and
methods of the existing class into the new class
to make them available in the new class or we
can simply inherit the old class in the new
class. 27
Inheritance in PHP
Syntax:
In PHP, extends keyword is used to specify
the name of the parent class while defining the
child class.
<?php
class Student
{
// parent class code
}
class CO extends Student
{
// child class code
28
By: Bhojankar M. N.
Inheritance in PHP
class ME extends Student
{
// child class code
}
?>
29
By: Bhojankar M. N.
Hirarchical Inheritance
<?php // inherit.php
//Program to demonstrate the multiple
inheritance
// parent class
class Student
{
// public property name
public $name;
$c->cotheoryexam();//Calling co sub
class method
$c->copracticalexam();
$m->metheoryexam();//Calling
33
By: Jadhav S. B.
me sub
class method
Multiple Inheritance
Output:
34
By: Bhojankar M. N.
Object cloning
Object cloning is the process to create a copy
of an object.
Cloning in PHP is maintained by creating a
shallow copy of the object.
We can create copy of an object using” =”
operator than why to use object cloning.
When you have created a copy of an object
using “=” operator. The reference of both the
object (original and the one which is created)
has the same reference.
After copying, when you will change the value
of the variable of one object then other object
will automatically gets changed.
35
By: Bhojankar M. N.
Object cloning
<?php // clone1.php
//Program to demonstrate the object copy
class employee {
private $name;
public function text($emp_name) {
$this->name = $emp_name;
}
public function Name() {
return $this->name;
}
}
36
By: Bhojankar M. N.
Object cloning
$obj1 = new employee();
$obj1->text("Vijay");
By: Bhojankar M. N.
Object cloning
<?php // clone2.php
//Program to demonstrate the object copy
class employee {
private $name;
public function text($emp_name) {
$this->name = $emp_name;
}
public function Name() {
return $this->name;
}
}
39
By: Bhojankar M. N.
Object cloning
$obj1 = new employee();
$obj1->text("Neha");
$obj2->text("Ragini");
echo $obj1->Name()."</br>";
echo $obj2->Name();
?>
Output:
Neha
Ragini 40
By: Bhojankar M. N.
Introspection:
Introspection is a common feature in any
programming language which allows object
classes to be manipulated by the programmer.
PHP offers a large number functions that you
can use to accomplish the Introspection.
There are some handful functions to implement
introspection of PHP’s.
class_exists() –>
Takes class name as string and returns
a boolean value depending on whether that class
exist or not.
$existence_status =
class_exists(classname); 41
By: Bhojankar M. N.
Introspection:
get_declared_class() returns an array classes
defined using this array we can check if a
perticular class exist or not.
Eg:
$classes = get_declared_classes();
M. N.
names in a class
Introspection:
get_parent_class() returns the parent class
name of class as argument.
$superclassName = get_parent_class(class
and);
By: Bhojankar M. N.
Introspection:
?php //introspec.php
//php script to implement the concept of
introspection
class Introspection
{
public function description()
{
echo "I am a super class for the Child
class.",'<br>';
}
}
44
By: Bhojankar M. N.
Introspection:
class Child extends Introspection{
public function description()
{
echo "I'm " . get_class($this) , "
class.",'<br>';
echo "I'm " . get_parent_class($this) ,
"'s child.",'<br>';
}
}
if (class_exists("Introspection"))
{
$introspection = new Introspection();
echo "The class name is: " .
get_class($introspection),'<br>';
45
By: Jadhav S. B.
Introspection:
if (class_exists("Child")) {
$child = new Child();
$child->description();
if (is_subclass_of($child,
"Introspection"))
{
echo "Yes, " . get_class($child) . " is a
subclass of Introspection.";
}
else
{
echo "No, " . get_class($child) . " is not
a subclass of Introspection.";
} 46
By: Jadhav S. B.
Introspection:
Output:
47
By: Bhojankar M. N.
Introspection:
//introspec1.php
Output:
48
By: Bhojankar M. N.
Serialization:
Serializing an object means converting it to a
bytestream representation that can be stored in
a file.
This is useful for persistent data;
For example, PHP sessions automatically save
and restore objects.
Serialization in PHP is mostly automatic—it is
requires to call the serialize( ) and
unserialize( ) functions:
$encoded = serialize(something);
$something = unserialize(encoded);
49
By: Bhojankar M. N.
serialize() Function
The serialize() function converts a storable
representation of a value.
To serialize data means to convert a value to a
sequence of bytes, so that it can be stored in a
file, a memory buffer, or transmitted across a
network.
Syntax:
serialize(value);
Where,
Serialize() function It returns a string that
contains a byte-stream representation of value.
50
By: Bhojankar M. N.
serialize() function
<?php
$data = serialize(array("Red",
"Green", "Blue"));
echo $data;
?>
Output:
51
By: Bhojankar M. N.
unserialize() Function
The unserialize() function converts serialized
data back into actual data.
Syntax:
unserialize(string);
52
By: Bhojankar M. N.
unserialize() function
<?php
$data = serialize(array("Red",
"Green", "Blue"));
echo $data;
?>
Output:
53
By: Bhojankar M. N.