[go: up one dir, main page]

0% found this document useful (0 votes)
13 views53 pages

Unit-III Apply Object Oriented Concepts in PHP

The document outlines the application of object-oriented concepts in PHP, including class definitions, object creation, member functions, constructors, destructors, inheritance, and introspection. It provides examples of creating classes and objects, using constructors for initialization, and implementing inheritance for code reuse. Additionally, it discusses object cloning and the importance of avoiding reference issues when copying objects.

Uploaded by

kaverimusale380
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views53 pages

Unit-III Apply Object Oriented Concepts in PHP

The document outlines the application of object-oriented concepts in PHP, including class definitions, object creation, member functions, constructors, destructors, inheritance, and introspection. It provides examples of creating classes and objects, using constructors for initialization, and implementing inheritance for code reuse. Additionally, it discusses object cloning and the importance of avoiding reference issues when copying objects.

Uploaded by

kaverimusale380
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Unit-III 16 Marks

Apply Object Oriented


Concepts in PHP
CO.c : Develop program by applying various object
oriented concept.

By: Bhojankar M. N. 1
Unit-III 16 Marks

Apply Object Oriented


Concepts in PHP
Objectives
• Develop program using control statement.
• Perform operations based on arrays and graphics.
• Develop programs by applying various object oriented concepts.
• Use form controls with validation to collect users input.
• Perform database operations in PHP.

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();

 Here we have created three objects and these


objects are independent of each other and they
will have their existence separately.
8

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= new Subject();

$php->set_name('PHP');

echo $php->get_name();
echo "<br>";
?>
12
By: Bhojankar M. N.
PHP Class
Output:

Assignment: WA PHP script to create a class Book


with data members as name & price. set &
display the information for 3 book.

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.

 Parameterized Constructor: It takes the


parameters, and also you can pass different
values to the data members.

 Copy Constructor: It accepts the address of


the other objects as a parameter.
19

By: Bhojankar M. N.
Calling Constructor
<?php classdemo2.php
class Book
{
public $name;
public $price;

function __construct($name, $price)


{
$this->name=$name;
$this->price=$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;

// public function walk


public function onlineexam() {
echo $this->name. " is giving
online exam...<br/>";
} 30
By: Jadhav S. B.
}
Hirarchical Inheritance
// child class
class CO extends Student
{
public function cotheoryexam()
{
echo $this->name. " is giving co
theory exam...<br/>";
}
public function copracticalexam()
{
echo $this->name. " is giving co
practical exam...<br/>";
}
} 31
By: Bhojankar M. N.
Hirarchical Inheritance
// child class
class ME extends Student
{
public function metheoryexam()
{
echo $this->name. " is giving
me theory exam...<br/>";
}
public function
mepracticalexam()
{
echo $this->name. " is giving
me practical exam...<br/>";
} 32
By: Jadhav S. B.
}
Hirarchical Inheritance
$c= new CO();
$c->name="CORaj";
$m= new ME();
$m->name="MERaj";

// calling Student class methods


$c->onlineexam();//Calling super class
method
$m->onlineexam();

$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");

$obj2 = $obj1; //only reference or memory


assigned to $obj2
$obj2->text("Ragini");

echo $obj1->Name()."</br>"; // Neha will be


replaced by Ragini
echo $obj2->Name();
?>
Output:
Ragini // $obj1 displays Ragini
Ragini // $obj2 also display Ragini
37
By: Bhojankar M. N.
Object cloning
 As in the above example, we can say that when
the object is copied directly it is copied by
reference, not by value.
 It means when you have changed the main
object value then copied object is also gets
affected and also when you will change the
value of copied object then the main object
value will also gets changed.
 This problem is solved by object cloning in
which object will never have the reference of
main object or original object.
 To clone an object means to create a similar
object with different reference.
38

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 = clone $obj1; //only reference or


memory assigned to $obj2

$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();

get_class_methods() returns an array of method


name defined in class and also which are
inherited from superclass.
$methodNames =
get_class_methods(classname);
get_class_vars() returns an associative array of
in which keys are theBy: Bhojankar
property
42

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);

get_class_methods()& get_class_vars() returns


properties of object & methods.

Is_Object() It is used to check if the parameter


passed is an object or not, it returns a boolean
value.
$object_status = is_object(obj);
43

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);

The above unserialze() function converts the


data from byte stream format into string.

52

By: Bhojankar M. N.
unserialize() function
<?php
$data = serialize(array("Red",
"Green", "Blue"));
echo $data;
?>
 Output:

53
By: Bhojankar M. N.

You might also like