Unit 3 Apply Object Oriented Concepts in PHP-2
Unit 3 Apply Object Oriented Concepts in PHP-2
Creating classes:-
A class is defined by using the class keyword, followed by the name of the class and a pair of
curly braces ( { } ).
Syntax :-
<?php
class Student
{
// code goes here... properties and methods
}
?>
Creating objects:-
To create an object of a given class
We use “new” keyword followed by class name for which we want to create object
Syntax:-
$object_name=new class_name;
Example:-
If we are having class Student as shown above then we create stud object for class student
$stud=new Student;
i.e stud is object of class Student.
Example
<?php
class Student
{
// Properties
public $name;
public $rno;
// Methods
function set_name($name)
{
$this->n1 = $name;
}
function get_name()
{
return $this->n1;
}
//for Roll No
function set_roll_no($rno)
{
$this->r1 = $rno;
}
function get_roll_no()
{
return $this->r1;
}
$s1->set_name('Ram');
echo $s1->get_name();
echo "<br>";
$s1->set_roll_no(1);
echo $s1->get_roll_no();
echo "<br>";
?>
Output:-
$this Keyword :-
The $this keyword refers to the current object, and is only available inside methods.
1. Inside the class (by adding a set_name() method and use $this):
Example
<?php
class Student
{
public $name;
function set_name($name)
{
$this->n1 = $name;
}
?>
Example
<?php
class Student
{
public $name;
}
PHP - instanceof
You can use the instanceof keyword to check if an object belongs to a specific class:
Example
<?php
?>
1.public
2. private
3.protected.
Public :- A class member declared with public keyword is accessible from anywhare.
Protected :-A protected member is accessible from within its class and by inheriting
class.
Private :-private member can only be accessed by the same class in which it is defined
and is not visible to anything outside it.
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.
We see in the example below, that using a constructor saves us from calling the
set_name() method which reduces the amount of code:
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.
If the parent class has constructor defined in it, it can be called within child class's constructor
by
1. construct
2. parent::__construct
However, if child class doesn't define a constructor, it inherits the same from is base class.
Program:-
<?php
class Student
{
// Properties
public $name;
public $rno;
// Methods
function construct ($name) // constructer
{
$this->n1 = $name;
}
function get_name()
{
return $this->n1;
}
?>
Output:-
// Methods
function Student() //class == constructer
{
echo " constructer having name as class name Student";
}
function construct() //1
{
echo "Student constructer having name construct"."<br>";
}
If you create a destruct() function, PHP will automatically call this function at the end
of the script.
PHP code is executed completely by its last line by using PHP exit() or die() functions.
As constructors and destructors helps reducing the amount of code, they are very useful!
Program:-
<?php
class Student
{
// Properties
public $name;
public $rno;
// Methods
function construct($name) //constructor
{
$this->n1 = $name;
}
function get_name()
{
return $this->n1;
}
Inheritance:-
Inheritance is the way of extending the existing class functionality in the newly created
class.
We can also add some additional functionality to the newly created class apart from
extending the base class functionalities.
When we inherit one class, we say an inherited class is a child class (sub class)
PHP supports various types of inheritance like JAVA/C++. The below table shows the list of
inheritance types and the supporting status in PHP.
Multiple Inheritance NO
PHP supports Single inheritance. Single inheritance is a concept in PHP in which one class can
be inherited by a single class only. We need to have two classes in between this process. One is
the base class (parent class), and the other a child class itself. Let‟s understand the same with an
example. It is popularly known as simple inheritance. This type of inheritance in PHP
language remains the same as JAVA, C++, etc
2. Multilevel Inheritance
PHP supports Multilevel Inheritance. In this type of inheritance, we will have more than 2
classes. In this type of inheritance, a parent class will be inherited by a child class then that child
class will be inherited by the child class. This type of inheritance in PHP language remains the
same as C++ etc.
PHP doesn‟t support multiple inheritance but by using Interfaces in PHP or using Traits in PHP
instead of classes, we can implement it.
Classes, case classes, objects, and traits can all extend no more than one class but can extend
multiple traits at the same time.
3. Hierarchical Inheritance
$c1=new c();
$c1->showc();
$c1->showa();
?>
Que.Write a Program to implements following types inheritance using given classes with its
properties
1. Single
2. Multilevel
3. Hierarchical
Program:-
<?php
}
}
$b1=new b();
$b1->show();
?>
Method overriding occurs in two classes that have IS-A (inheritance) relationship.
Program:-
<?php
}
}
$b1=new b();
$b1->show();
$a1=new a();
$a1->show();
?>
When object is cloned PHP will perform a shallow copy of the objects properties
Syntax
$new_object_name=clone $old_object_to_be_copied
Example
Examining classes:
Introspection Functions to be used to examining classes are,
1. class_exists ()
2. get_class_methods ()
3. get_class_vars ()
4.get_parent_class ()
1. class_exists ()
Syntax
get_declared_classes ()
Syntax
2. get_class_methods ()
Syntax
3. get_class_vars ()
Syntax
4. get_parent_class ()
This function Returns an name of parent class or false if there is no parent class
Syntax
Program:-
<?php
class a
{
//properties of class (variables names)
public $name;
public $rno;
//Methods
function sum ()
{
}
function add ()
{
}
function sub ()
{
}
}
class c extends b
{
//1 class_exists
if(class_exists('a'))
{
echo "Class presnt";
echo "<br>";
}
else
{
echo "Class not presnt";
echo "<br>";
}
//2 get_class_methods
$cm=get_class_methods ('b');
echo "class methods names :- ";
print_r($cm);
echo "<br>";
//3 get_class_vars
$cv=get_class_vars ('a');
echo "variable names :- ";
var_dump($cv);
//4 get_parent_class
echo "<br>";
$p=get_parent_class('c');
echo "parent class :- ";
print_r($p);
?>
Examining an Object:
Introspection Functions to be used to examining an object are,
1. is_object ()
2. get_class ()
3. get_object_vars ()
4. method_exists ()
1. is_object ():-
Syntax
$yesno=is_object ($obj);
2. get_class ()
Syntax
3. get_object_vars ()
Syntax
Syntax
Program:-
class a
{
//properties of class (variables names)
public $name;
public $rno;
//Methods
function sum ()
{
}
function add ()
{
}
function sub ()
{
}
//inheritance
class b extends a
{
function Show()
{
}
}
class c
{
public $comp_name;
public $Branch;
}
//1 is_object
$obj=is_object($a1);
echo "object or not :- ";
var_dump($obj);
echo "<br>";
//2 get_class
$cm=get_class ($b1);
echo "class names :- ";
print_r($cm);
echo "<br>";
//3 get_object_vars
$objv=get_object_vars ($c1);
echo "variable names :- ";
var_dump($objv);
echo "<br>";
//4 method_exists
$p=method_exists($c1,'add');
echo "parent class :- ";
var_dump($p);
?>
Output:-
To serialize data means to convert a value to a sequence of bits, so that it can be stored in
a file, a memory buffer, or transmitted across a network.
We can use two functions serialize ( ) and unserialize() to implement our own form of
persistent object
For example
Unserialize () method can use this string to recreate the original variable values
Syntax
serialize(value);
While saving an object, all the variables of the object will be saved, but the functions will
not be saved.
To unserialize () an object in another PHP file, the class must be defined in that file.
This can be done by including rile where the class has been defined
Syntax
unserialize( value)
Program:-
<?php
class Student
function show_Age()
$stud->show_Age();// Outputs 10
fclose ($fp);
$us_obj->show_Age(); // outputs 10
?>
PHP has two hooks for objects during the serialization and unserialization process
1. sleep( )
2. wakeup()
These methods are used to notify objects that they are being serialized or unserialize.
Serialize() checks if our class has a function with the magic name sleep(), If so, that
function is executed prior i.e. just before to any serialization.
It can clean up the object and is supposed to return an array with the names of all
variables of that object that should be serialized.
if the method doesn't return anything then Null is serialized. The sleep() function is
useful if we have very large objects which do not heed to be saved completely.
Unserialize() checks for the presence of a function with the magic name wakeup(). if
present, this function can reconstruct any resources that the object may have.
<?php
class Student
{
public $name;
public $rno;
public function construct($name, $rno)
{
$this->name = $name;
$this->rno = $rno;
}
public function sleep()
{
echo "Sleep calling <br>";
return array('name','rno');
}
public function wakeup()
{
echo "Wakeup calling <br>";
}
}
$s1=new Student("Ram",01);
$data= serialize($s1);
echo $data."<br>";
$un=unserialize($data);
print_r($un);
?>
Output:-