0% found this document useful (0 votes)
118 views20 pages

Unit Iii

The document discusses object oriented programming concepts in PHP including: - Creating classes with the class keyword and defining properties and methods. - Creating objects using the new keyword. - Accessing object properties and methods using ->. - Constructors for initializing objects. - Destructors for cleaning up objects. - Inheritance using the extends keyword to derive new classes. - Method overriding by redefining methods in child classes. - Cloning objects using the clone keyword. Examples are provided to illustrate each concept.

Uploaded by

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

Unit Iii

The document discusses object oriented programming concepts in PHP including: - Creating classes with the class keyword and defining properties and methods. - Creating objects using the new keyword. - Accessing object properties and methods using ->. - Constructors for initializing objects. - Destructors for cleaning up objects. - Inheritance using the extends keyword to derive new classes. - Method overriding by redefining methods in child classes. - Cloning objects using the clone keyword. Examples are provided to illustrate each concept.

Uploaded by

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

UNIT-III : Apply object oriented Concept in PHP

**************************************************
* Class Name : VJTech Academy , Maharashtra *
* Author Name: Vishal Jadhav Sir *
* Mobile No : 9730087674 *
**************************************************
- Creating classes:
- Syntax:

class className
{
variable declarations....
function definitions....
}

- Creating object:
- Syntax:

$objectName=new className();

- Accessing properties and methods of the class:


- Syntax:

$object->property_name;
$object->method_name(args list);
Program-1:
<?php
class Student

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

{
var $rollno;
var $name;
var $marks;
function get_stud_info()
{
$this->rollno=1010;
$this->name="Vishal";
$this->marks=89.99;
}
function disp_stud_info()
{
echo "Student Roll No:".$this->rollno;
echo "Student Name :".$this->name;
echo "Student Marks :".$this->marks;
}
}
$s1=new Student();
$s1->get_stud_info();
$s1->disp_stud_info();
?>

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

Program-2:
<?php
class Book
{
var $bookid;
var $book_name;
var $book_price;
function get_book_info()
{
$this->bookid=readline("Enter Book ID:");
$this->book_name=readline("Enter Book Name:");
$this->book_price=readline("Enter Book Price:");
}
function disp_book_info()
{
if($this->book_price>500)
{
echo "\nBook ID :".$this->bookid;
echo "\nBook Name :".$this->book_name;
echo "\nBook Price :".$this->book_price;
}
}
}
$b1=new Book();

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

$b2=new Book();
$b3=new Book();

$b1->get_book_info();
$b2->get_book_info();
$b3->get_book_info();

$b1->disp_book_info();
$b2->disp_book_info();
$b3->disp_book_info();
?>
==============
Constructor
==============
• Constructor is a special method of your class.
• It will called automatically when object is created.
• It is used for object creation.
• Constructor is used to initialize the properties of objects.
• To add constructor in class,we just simply add special method
with the name _ _construct() or use same name as class name.
- Example1:
<?php
class AreaOfCircle
{
var $radius;

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

var $PI;
function __construct()
{
$this->radius=readline("Enter Radius of circle:");
$this->PI=3.14;
}
function disp_Area_Of_Circle()
{
$area=($this->PI*$this->radius*$this->radius);
echo "Area of circle = $area";
}
}
$a1=new AreaOfCircle();
$a1->disp_Area_Of_Circle();
?>
- Example2:
<?php
class AreaOfCircle
{
var $radius;
var $PI;
function AreaOfCircle($r)
{
$this->radius=$r;

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

$this->PI=3.14;
}
function disp_Area_Of_Circle()
{
$area=($this->PI*$this->radius*$this->radius);
echo "Area of circle = $area";
}
}
$a1=new AreaOfCircle(2);
$a1->disp_Area_Of_Circle();
?>
==============
Destructor
==============
• Destructor is a special method of your class.
• It will called automatically when there is no any reference to
the object.
• It is used for object destruction.
• We can clean up the resources allocated for the object.
• Destructor never takes parameters.
• To add desctructor to a clas, we just use method name called
_ _destruct() function.
- Example1:
<?php
class VJTech

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

function __construct()
{
echo "\nConstructor called...!!!";
}
function __destruct()
{
echo "\nDestructor called...!!!";
}
}
$v1=new VJTech();
?>

OUTPUT:
Constructor called...!!!
Destructor called...!!!

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

==================
Inheritance
==================
• The process of creating new class from old class is known as
Inheritance.
• The machinsm of deriving the properties of old class to new
class is known as Inheritance.
• Newly created class is called as sub-class,newclass,Derived
class,Child class.
• Old class is called as Super class, Old class, Base class, Parent
class.
• Because of inheritance, we can achieve reusability.
• Because of this reusability features, our development time got
saved and it will also save project cost.
• We use extends keyword for creating new class from old class.
- Syntax:
class DerivedClass extends BaseClass
{
//body of derived class
}
- Example:
<?php
class Base
{
function display()
{

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

echo "Hello ";


}
}
class Derived extends Base
{
function show()
{
echo "Friends!!!";
}
}
$d1=new Derived();
$d1->display();
$d1->show();
?>
===================
Method Overriding:
===================
• When Base class method name and derived class method name
are same then base class method overriden by derived class
method name.
• When we call that method then only derived class method
body got executed.
- Program:
<?php
class Base

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

{
function display()
{
echo "display method of Base class";
}
}
class Derived extends Base
{
function display()
{
echo "display method of derived class";
}
}
$d1=new Derived();
$d1->display();
?>
OUTPUT:
display method of derived class
✓ But if you want to invoke the base class method then we can
use below syntax:
BaseClassName::OverriddenMethodName();
- Example:
<?php
class Base

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

{
function display()
{
echo "display method of Base class";
}
}
class Derived extends Base
{
function display()
{
Base::display();
echo "\ndisplay method of derived class";
}
}
$d1=new Derived();
$d1->display();
?>
OUTPUT:
display method of Base class
display method of derived class

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

❖ Inheritance Practice Program:


<?php
class Area
{
public $radius;
function get_radius()
{
$this->radius=readline("Enter Radius of Circle:");
}
}
class Circle extends Area
{
var $PI;
function Calc_Area_Of_Circle()
{
$this->PI=3.14;
$Area=($this->PI*$this->radius*$this->radius);

echo "\nArea of Circle = $Area";


}
}
$c1=new Circle();
$c1->get_radius();
$c1->Calc_Area_Of_Circle();

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

?>
OUTPUT:
Enter Radius of Circle:2
Area of Circle = 12.56

=================
Cloning Object:
=================
• Object cloning is the process to create copy of object.
• We can create copy of object using clone keyword.
- Syntax:
$CopyObjectName=clone $OldObjectName;
- Program:
<?php
class Area
{
public $radius;
function get_radius()
{
$this->radius=readline("Enter Radius of Circle:");
}
}
class Circle extends Area
{

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

var $PI;
function Calc_Area_Of_Circle()
{
$this->PI=3.14;
$Area=($this->PI*$this->radius*$this->radius);

echo "\nArea of Circle = $Area";


}
}
$c1=new Circle();
$c2=clone $c1; //c2 object is a copy of c1 object
$c1->get_radius();
$c2->Calc_Area_Of_Circle();
?>
====================
Introspection in PHP
====================
• Introspection is the ability of a program to examine an objects
characteristics.
• Using introspection, we can perform do below things:
- Obtain the name of class to which an object belongs as well as
its member propertis and methods.
- Write generic debuggers,serializers and profilers etc.
- Introspection in PHP offers the useful ability to examine classes,
interfaces, properties and methods.

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

Examining Classes:
==================
• To examining the classes the introspective functions provided
by the PHP.
• For example: class_exist(),
get_class_methods(),get_class_vars(), get_parent_class() etc.

1) class_exists():
• This function is used to determine whether a class is exists or
not.
• This functions takes class name as parameter and return
boolean value.
- Syntax:
$VariableName=class_exists(ClassName);
- This function return true if given class is present otherwise
false.
- Example:
<?php
class VJTech
{
function display()
{
echo "This is class";
}
}
if(class_exists("VJTech"))

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

{
echo "Given class exists";
}
else
{
echo "Given class does not exists";
}
?>
OUTPUT:
Given class exists

2) get_class_methods():
• This function returns an array of class methods.
- Syntax:
$methods=get_class_methods(classname);
- Example:
<?php
class VJTech
{
function display()
{
echo "This is display method";
}
function show()

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

{
echo "This is show method";
}
function getdata()
{
echo "This is getdata method";
}
}
$m=get_class_methods(VJTech);
print_r($m);
?>
OUTPUT:
Array
(
[0] => display
[1] => show
[2] => getdata
)

3) get_class_vars():
• This function returns an array of class variables/Properties.
- Syntax:
$variables=get_class_vars(classname);

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

- Example:
<?php
class VJTech
{
var $rollno;
var $name;
var $marks;
function display()
{
echo "This is display method";
}

}
$m=get_class_vars(VJTech);
print_r($m);
?>
OUTPUT:
Array
(
[rollno] =>
[name] =>
[marks] =>
)

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

4) get_parent_class():
• This function returns name of the parent class.
- Syntax:
$ParentClass=get_parent_class(classname);
- Example:
<?php
class VJTech
{
var $rollno;
}
class Derived extends VJTech
{
var $name;
}
$m=get_parent_class(Derived);
echo "Parent class name is $m";
?>

OUTPUT:
Parent class name is VJTech

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-III : Apply object oriented Concept in PHP

Inspiring Your Success

VJTech Academy…

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)

You might also like