Professional Documents
Culture Documents
**************************************************
* 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();
$object->property_name;
$object->method_name(args list);
Program-1:
<?php
class Student
{
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();
?>
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();
$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;
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;
$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
function __construct()
{
echo "\nConstructor called...!!!";
}
function __destruct()
{
echo "\nDestructor called...!!!";
}
}
$v1=new VJTech();
?>
OUTPUT:
Constructor called...!!!
Destructor called...!!!
==================
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()
{
{
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
{
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
?>
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
{
var $PI;
function Calc_Area_Of_Circle()
{
$this->PI=3.14;
$Area=($this->PI*$this->radius*$this->radius);
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"))
{
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()
{
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);
- 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] =>
)
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
VJTech Academy…