0% found this document useful (0 votes)
31 views85 pages

Unit III

The document discusses a web based application development course with PHP. It provides details about the course like learning objectives, teaching scheme, units and topics to be covered, assignments, projects and outcomes. The key topics include expressions, control statements, arrays, functions, graphics, object oriented concepts, forms, validation and database operations in PHP.

Uploaded by

TAIBA SHAIKH
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)
31 views85 pages

Unit III

The document discusses a web based application development course with PHP. It provides details about the course like learning objectives, teaching scheme, units and topics to be covered, assignments, projects and outcomes. The key topics include expressions, control statements, arrays, functions, graphics, object oriented concepts, forms, validation and database operations in PHP.

Uploaded by

TAIBA SHAIKH
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/ 85

Program Title:- Computer Engineering

Sem/Year:- 6th (I Scheme)


Course Title:- Web Based Application development with PHP
Course Abbreviation:- WBP Course Code:- 22619
Level of understanding the subject :- Easy & Medium

Prerequis Knowledge about Web Page Designing and Proficiency in programming


ites language and analytical skills.
Learning Students will get the knowledge of web based
Objective Knowledge
application development.
(What
Students will understand the process of Server
will my Understanding
installation and use of Scripting language.
students
Students will perform various operation on
KNOW Application
Arrays and Graphics.
by the
end of the Students will learn to analyse programming
Analysis
subject?) constructs and apply appropriate one.

Student will be able to design form and validate


Evaluation
controls.
Student will be able to create database and
Create
perform database operations.
Teaching Scheme

Teaching
Theory Practical
Scheme
Credit
(L+T+P) Paper ESE PA TOTAL ESE PA TOTAL
L T P
hrs. Max Min Max Min Max Min Max Min Max Min Max Min

3 - 2 5 3 70 28 30* 0 100 40 25@ 10 25 10 50 20

Rationale :- To inculcate web based application development skills in students using


server side scripting.
Competency: Develop simple web based application using PHP Language.

Course Outcomes:
a. Develop Program using Control Statements.
b. Perform operations based on arrays and graphics.
c. Develop Program by applying various object oriented concepts.
d. Use form controls with validation to collect user’s input.
e. Perform database operations in PHP
Index
S.N. Description TH No. of
Understanding Marks Hrs Pr
Level
Units 70 48 16
1. Expressions & Control Statements in PHP Easy 12 06 3
2. Arrays, Functions & Graphics Medium 16 10 4
3. Apply Object Oriented Concepts in PHP Medium 16 12 2
1 4. Creating & Validating Forms Medium 12 12 5
5. Database Operations Medium 14 08 2
2 Assignments 5
50
3 Practice questions-Unit Tests
Questions
5Q-2M ,5Q-4M

4 List of Micro-project 10 ----- -----

5 Question papers & Models answer solution 4 ----- -----


Unit III: Apply Object Oriented Concepts in PHP
LO. Learning Outcomes/Unit Outcomes
No.

3a Write constructors and destructors function for the given problems in


PHP .

3b
Implement Inheritance to extends to given base class.
3c
Use overloading / overriding to solve the given problem.
3d
Clone the given object.
Class
- A class is defined by using the class keyword, followed by the
name of the class and a pair of curly braces ({}).
- All its properties and methods go inside the braces:
Syntax :

<?php
class classname {
// code goes here...
}
?>
<?php
class classname {
// Properties
//variable declaration
public $variable1;
public $variable2;
// Methods
function method1($variableN) {
$this->variable1 = $variableN;
}
function method2() {
return $this->variable1;
}
}
?>
Objects

- is a variable of type class


- is created to access properties of class(variables and methods) outside
the class.
- Classes are nothing without objects!
- We can create multiple objects from a class.
- variables and functions are accessed using -> operator

Syntax :

$objectname = new classname();

Example :

$apple = new Fruit();


$this Keyword:

The $this keyword refers to the current object, and is only available
inside methods.

Example
<?php
class Fruit {
public $name;
}
$apple = new Fruit();
?>
we can change the value of the variables of class (e.g.$name property) by two ways:

1. Inside the class :


Example
<?php
class Fruit {
public $name;
function set_name($name) {
$this->name = $name;
}
}
$apple = new Fruit();
$apple->set_name("Apple");
echo $apple->name;
?>
2. Outside the class :
Example
<?php
class Fruit {
public $name;
}
$apple = new Fruit();
$apple->name = "Apple";
echo $apple->name;
?>
Prog on Compiler
Prog using XAMPP
<?php
echo "Hello ,Welcome to OOP Concepts!<br>";
class emp
{
public $id;
public $n;
function get()
{
$this->id=100;
$this->n="abc";
}
function disp()
{
echo "ID : $this->id<br>";
echo "Name: $this->n<br>";
}
}
echo "Lets use emp class<br>";
$e=new emp();
$e->get();
$e->disp();
echo "Thank you!<br>";
?>
instanceof
We can use the instanceof keyword to check if an object belongs to a
specific class:

Syntax: objectname instanceof classname

Example
<?php
$apple = new Fruit();
var_dump($apple instanceof Fruit);
?>
● Constructor
- A constructor allows to initialize an object of class.
- It is automatically called when we create an object of
class.
- Constructor is created using a “ __construct()” function.
- It can be default or parameterized.
Syntax:
1. Constructor with no argument 2. Constructor with argument

function __construct ( ) function __construct ( parameter-list )

{ {
//code //code

} }
Constructor with Zero Argument

<?php
echo "Hello ,Welcome to OOP Concepts!<br>";
class emp
{
public $id;
public $n;
function __construct() //constructor with no argument
{
$this->id=200;
$this->n="abc";
}
function disp()
{
echo "ID : $this->id<br>";
echo "Name: $this->n<br>";
}

}
echo "Lets use emp class<br>";
$e=new emp(); //constructor will be called

$e->disp();
echo "Thank you!<br>";
?>
Constructor with Argument

<?php
echo "Hello ,Welcome to OOP Concepts!<br>";
class emp
{
public $id;
public $n;
function __construct($i,$nm) //constructor with argument
{
$this->id=$i;
$this->n=$nm;
}
function disp()
{
echo "ID : $this->id<br>";
echo "Name: $this->n<br>";
}

}
echo "Lets use emp class<br>";
$e=new emp(12,"xyz"); //constructor with arg will be called
$e->disp();
echo "Thank you!<br>";
?>
● Destructor
- A destructor is used to destroy the object or cleanup the object memory.
- called when the the script is exited. created using a ”__destruct()” function.
- PHP will automatically call this function at the end of the script.
- destruct function starts with two underscores (__)

Syntax:

function __destruct( )
{
//code

}
Destructor

<?php
echo "Hello ,Welcome to OOP Concepts!<br>";
class emp
{
public $id;
public $n;
function __construct($i,$nm) //constructor with argument
{ $this->id=$i;
$this->n=$nm;
}
function disp()
{
echo "ID : $this->id<br>";
echo "Name: $this->n<br>";
}
function __destruct()
{
echo "---Bye----";
}
}
echo "Lets use emp class<br>";
$e=new emp(12,"xyz"); //constructor will be called

$e->disp();
echo "Thank you!<br>";
?>
Access Modifiers
Properties and methods can have access modifiers which control
where they can be accessed.
There are three access modifiers:
● private - the property or method can ONLY be accessed
within the class
● public - the property or method can be accessed from
everywhere. This is default
● protected - the property or method can be accessed within
the class and by classes derived from that class
public access specifier for variable

<?php
echo "Hello ,Welcome to OOP Concepts!<br>";
class emp
{ public $id;
public $n;
function __construct($i,$nm) //constuctor with argument
{
$this->id=$i;
$this->n=$nm;
}
function disp()
{
echo "ID : $this->id<br>";
echo "Name: $this->n<br>";
}
}
echo "Lets use emp class<br>";
$e=new emp(12,"xyz"); //constructor will be called
$e->disp();
$e->id=300; //public variables are accessible using object
$e->n="LMN";
$e->disp();
echo "Thank you!<br>";
?>
public access specifier for function

<?php
echo "Hello ,Welcome to OOP Concepts!<br>";
class emp
{ public $id;
public $n;
function __construct($i,$nm) //constuctor with argument
{
$this->id=$i;
$this->n=$nm;
}
public function disp()
{
echo "ID : $this->id<br>";
echo "Name: $this->n<br>";
}
}
echo "Lets use emp class<br>";
$e=new emp(12,"xyz"); //constructor will be called
$e->disp(); //public function accessible using object
$e->id=300;
$e->n="LMN";
$e->disp();
echo "Thank you!<br>";
?>
private access specifier for variable

<?php
echo "Hello ,Welcome to OOP Concepts!<br>";
class emp
{ private $id;
public $n;
function __construct($i,$nm) //constuctor with argument
{
$this->id=$i;
$this->n=$nm;
}
function disp()
{
echo "ID : $this->id<br>";
echo "Name: $this->n<br>";
}
}
echo "Lets use emp class<br>";
$e=new emp(12,"xyz"); //constructor will be called
$e->disp();
$e->id=300; //privare variables not accessible using object
$e->n="LMN";
$e->disp();
echo "Thank you!<br>";
?>
● Inheritance
- When a class derived from another class.
- The child class will inherit all the public and protected properties and methods from the
parent class.
- An inherited class is defined by using the extends keyword.
Syntax:
<?php
class baseclassname { //code
}
class derivedclassname extends baseclassname
{ //code
}
//code
?>
Single Inheritance

Example:
<?php
class A
{ public $n;
function set()
{ $this->n="Priti";
}
}
class B extends A
{ function disp()
{ echo "Welcome $this->n";
A
}
}
$b=new B();
$b->set(); B

$b->disp();
?>
Multilevel Inheritance

Example:
<?php
class A
{public $n;
function set(){ $this->n="Priti"; }
}
class B extends A
{function disp(){ echo "Welcome $this->n<br>"; }
A
}
class C extends B
{function show() { echo "Bye $this->n"; }
} B
$ob=new C();
$ob->set();
$ob->disp();
C
$ob->show();
?>
Hierarchical Inheritance

<?php
class A
{public $n;
function set()
{ $this->n="Priti"; }
}
class B extends A
{
function disp() { echo "Welcome $this->n<br>"; }
}
class C extends A A
{function show()
{ echo "Bye $this->n";
}
}
B C
$ob1=new B();
$ob2=new C();
$ob1->set(); //B class has called function set so its not working for ob2 object of C class
$ob1->disp();
$ob2->show();
?>
Hierarchical Inheritance

<?php
class A
{public $n;
function set()
{ $this->n="Priti"; }
}
class B extends A
{
function disp() { echo "Welcome $this->n<br>"; }
}
class C extends A A
{function show()
{ echo "Bye $this->n";
}
}
B C
$ob1=new B();
$ob2=new C();
$ob2->set(); //C class has called function set so its not working for ob1 object of B class
$ob1->disp();
$ob2->show();
?>
Method Overriding :
Method overriding means when a base and derived class having same method with same
signature;then the method in derived class overrides the method in base class.

The method in the base/parent class is called overridden method, while the method in the
child/derived class is known as the overriding method. The code in the overriding
method overrides (or replaces) the code in the overridden method.

PHP will decide which method (overridden or overriding method) to call based on the
object used to invoke the method.

● If an object of the parent class invokes the method, PHP will execute the overridden
method.
● But if an object of the child class invokes the method, PHP will execute the
overriding method.
<?php
class A
{
public $n;

function show()
{ echo "Hi";
}
}
class B extends A
{
function show()
{ echo "Bye";
}
}

$ob=new B();
$ob->show();
?>
<?php
class A
{
public $n;

function show()
{ echo "Hi<br>";
}
}
class B extends A
{
function show()
{ parent::show(); //calls show() function of parent/super/base class
echo "Bye<br>";
}
}

$ob=new B();
$ob->show();
?>
The final Keyword

The final keyword can be used to prevent method overriding.


Syntax:
<?php
class baseclass
{
final function function-name()
{ // code
}
}
class derived extends baseclass
{
function function-name() // will result in error
{
// some code
}
}
?>
<?php
class A
{
public $n;
final function show()
{
echo "Hi<br>";
}
}
class B extends A
{
function show() //gives error
{
echo "Bye<br>";
}
}
$ob=new B();
$ob->show();
?>
The final Keyword

The final keyword can be used to prevent class inheritance.

Syntax:

<?php
final class base-class-name

{ // code
}

class derived-class-name extends base-class-name // will result in error

{ // code
}
?>
<?php
final class A
{
public $n;
function show()
{
echo "Hi<br>";
}
}
class B extends A //gives error
{
function show()
{
echo "Bye<br>";
}
}
$ob=new B();
$ob->show();
?>
Constants
● Constants can be useful if you need to define some constant data within a class.
● A constant is declared inside a class with the const keyword.
● A constant cannot be changed once it is declared.
● Class constants are case-sensitive.
● it is recommended to name the constants in all uppercase letters.
We can access a constant from inside the class by using
the self keyword followed by the scope resolution operator (::)
followed by the constant name, like here:
<?php
class Goodbye {
const MESSAGE = "Thank you for visiting!";
public function bye() {
echo self::MESSAGE;
}
}
$g = new Goodbye();
$g->bye();
?>
We can access a constant from outside the class by using the class
name followed by the scope resolution operator (::) followed by the
constant name:

<?php
class Goodbye {
const MESSAGE = "Thank you for visiting!";
}
echo Goodbye::MESSAGE;
?>
Abstract Classes
● An abstract class is a class that contains at least one abstract method.
● Abstract classes and methods are when the parent class has a named
method, but need its child class(es) to fill out the tasks.
● An abstract method is a method that is declared, but not implemented in
the code.
● Example:
Shape
area( )

Circle Rectangle
area( ) area( )
● An abstract class or method is defined with the abstract keyword.
abstract class Car

So, when a child class is inherited from an abstract class, we have the following
rules:

● The child class method must be defined with the same name and it redeclares
the parent abstract method
● The child class method must be defined with the same or a less restricted access
modifier.
Characteristics of abstract class:
• It can have both regular methods( with implementations) and abstract
methods (without any implementation. )
• They serve as placeholders, ensuring that any class extending the abstract class
provides an implementation for those methods.
• Abstract class in php are useful when you want to define a common interface or
behavior that multiple related classes should have.
• They provide a level of abstraction.
• Object of abstract class can not be created.
<?php
abstract class Student
{
public $rn;
public function setRollNo($rollno)
{
$this->rn = $rollno;
echo "Roll No:$this->rn <br>";
}
abstract public function favouriteSubject();
}
class ABC extends Student
{
public function favouriteSubject()
{
echo " favourite Course : English";
}
}
$obj = new ABC; //this statement does not throws error and executes correctly
$obj->setRollNo(12);
$obj->favouriteSubject();
?>
<?php
class shape
{
public $r;
function area(); //trying to declare method and not writing body
}
class circle extends shape
{
function area()
{
$this->r=4;
$ac=3.14*$this->r*$this->r;
echo "Area of circle: $ac";
}
}
class rectangle extends shape
{
function area()
{
$this->l=4;
$this->b=3;
$ar=$this->l*$this->b;
echo "Area of Square: $ar";
}
}
echo "----------Calculating Area of Shape----<br>";
$c=new circle();
$c->area();
$r=new rectangle();
$r->area();
echo "--------Thank you----------<br>";
?>
<?php
class shape
{
public $r;
abstract function area(); //declaring method as abstract
}
class circle extends shape
{
function area()
{
$this->r=4;
$ac=3.14*$r*$r;
echo "Area of circle: $ac";
}
}
class rectangle extends shape
{
function area()
{
$this->l=4;
$this->b=3;
$ar=$l*$b;
echo "Area of Square: $ar";
}
}
echo "----------Calculating Area of Shape----<br>";
$c=new circle();
$c->area();
$r=new rectangle();
$r->area();
echo "--------Thank you----------<br>";
?>
<?php
class shape
{
public $r;
abstract function area(); //declaring method as abstract
}
class circle extends shape
{
function area()
{
$this->r=4;
$ac=3.14*$this->r*$this->r;
echo "Area of circle: $ac";
}
}
class rectangle extends shape
{
function area()
{
$this->l=4;
$this->b=3;
$ar=$this->l*$this->b;
echo "Area of Square: $ar";
}
}
echo "----------Calculating Area of Shape----<br>";
$c=new circle();
$c->area();
$r=new rectangle();
$r->area();
echo "--------Thank you----------<br>";
?>
<?php
abstract class shape //declared class as abstract
{ public $r;
abstract function area(); //declared function as abstract
}
class circle extends shape
{ function area()
{ $this->r=4;
$ac=3.14*$this->r*$this->r;
echo "Area of circle: $ac<br>";
}
}
class rectangle extends shape
{
function area()
{ $this->l=4;
$this->b=3;
$ar=$this->l*$this->b;
echo "Area of Square: $ar<br>";
}
}
echo "----------Calculating Area of Shape----<br>";
$c=new circle();
$c->area();
$r=new rectangle();
$r->area();
echo "--------Thank you----------<br>";
?>
Multiple Inheritance

A B
Show() Show()

C
Show()

As per Inheritance,we will create object of Derived class:


$ob=new C( );
$ob->Show( ) ; //this line will create ambiguity which Show( ) to call
//because both base classes are having Show( )
Multiple Inheritance

A B
Show() Show()

C
Show()

Why this is happening?


Because both are classes and classes need to define function.
Once concept is similar to class which allows only function declaration:

Interface
Interfaces
● Interfaces allow you to specify what methods a class should implement.
● Interfaces are declared with the interface keyword:
Syntax
<?php
interface InterfaceName {
public function someMethod1();
public function someMethod2();

}
?>
To implement an interface, a class must use the implements keyword.
class classname implements interfacename {
public function someMethod1() {
//code
}
}
<?php
interface WebApp
{
public function login($email, $password);
public function logout();
}
class testapp implements WebApp
{
public function login($email, $password)
{ echo "Login the user with email: " . $email;
}
public function logout()
{ echo "<br>User logged out!";
}
}
$ob=new testapp();
$ob->login("[email protected]",123);
$ob->logout();
?>
<?php
class A
{
function show()
{ echo "Hi<br>";
}
}
interface B
{ function show();
}
class C extends A implements B
{
function show()
{ echo "Bye<br>";
}
}
$ob=new C();
$ob->show();
echo "--------Thank you----------<br>";
?>
Interface Characteristics:
•All the methods declared in the interfaces are public and do not
start with the abstract keyword.
•If we miss to implement even a single method declared in the
interface, in the class implementing the interface, we will get an
error.
•Interfaces do not have variables.
Interface Class

Interfaces cannot have properties abstract classes can have properties

All interface methods must be public abstract class methods is public or


protected

All methods in an interface are abstract All methods need not to be an abstract

It is implemented using implements Its extended using extends keyword


keyword
Traits
- PHP only supports single inheritance: a child class can inherit only from one
single parent.
- So, what if a class needs to inherit multiple behaviors? traits solve this
problem.
- Traits are used to declare methods that can be used in multiple classes.
- Traits can have methods and abstract methods that can be used in multiple
classes, and the methods can have any access modifier (public, private, or
protected).
Example:
Syntax : <?php
<?php trait t1
trait TraitName {
{ public function msg1()
// some code... {
} echo "PHP is fun! ";
?> }
}
Example
To use a trait in a class, use <?php
the use keyword: trait t1 {
public function msg1() {
Syntax: echo "PHP is fun! ";
}
<?php }
class MyClass { class demo {
use TraitName; use t1;
} }
?> $obj = new demo();
$obj->msg1();
?>
Traits Interface

Traits are more suitable for code reuse across Interfaces are more suitable for defining
unrelated classes. contracts and achieving abstraction and
polymorphism.
Traits allow for horizontal code reuse, Interfaces provide a way to achieve vertical
meaning that they enable the sharing of code code reuse, allowing classes to implement
between unrelated classes. multiple interfaces.

Traits can define methods, properties, or both. Interfaces can only define method signatures,
not properties.
Traits promote cleaner, more maintainable Using interfaces promotes loose coupling and
code by reducing code duplication and abstraction.
enabling the composition of behavior.

Traits offer flexibility as they can be easily Interfaces enforce good programming
added or removed from classes. practices, and design patterns, and improve
testability.
You can use multiple traits in a single class. One class can implement multiple Interfaces
Static variable
Static variables are declared with ‘static’ keyword.

It is declared to keep value of variable as a common to all objects of class.

Syntax :
<?php
class ClassName
{
public static $var=value;
public function Methodname( )
{ //code
}
}
?>
Static variable Characteristics:

1. Static variables are declared with ‘static’ keyword.

2. It is declared to keep value of variable as a common to all objects of class.

3. These variables are accessed using classname::$variablename inside and


outside class.

4. Only one copy of static variable exists.


<?php
class circle
{
public $r;
public static $pi =3.14;
public function area() {
$r=3;
echo "Answer = ".circle::$pi*$r*$r;
}
}
echo "Area of circle : <br>";
$c1=new circle();
$c1->area();
echo "<br>Thank You !<br>";
?>
Static Methods
Static methods can be called directly - without creating an instance of the class first.

Static methods are declared with the static keyword:

Syntax :
<?php
class ClassName
{
public static function Methodname( )
{
//code
}
}
?>
To access a static method use the class name, double colon
that is scope resolution(::), and the method name:
Syntax
ClassName::Methodname();

It means there will be only one copy of static function, no any further
function can have same name and same signature
<?php
class SI
{ public $p; //instance
public $n; //instance
public static $r=8.5; //static property
public function set($a,$y) // non-static function
{
$this->p=$a;
$this->n=$y;
}
public function cal() //non-static function
{ $ans=($this->p*$this->n*SI::$r)/100;
echo "<br>Simple Interest : $ans<br>";
}
}
echo "<br> welcome to Bank <br>";
$c1=new SI(); non-static function can
$c1->set(200000,3);
$c1->cal(); access instance and static
$c2=new SI();
$c2->set(800000,4); properties(variables)
$c2->cal();
echo "<br> Thank You <br>";
?>
<?php
class SI
{ public $p; //instance
public $n; //instance
public static $r=8.5; //static property
public static function set($a,$y) // static function
{
$this->p=$a;
$this->n=$y;
}
public function cal() //non-static function
{ $ans=($this->p*$this->n*SI::$r)/100;
echo "<br>Simple Interest : $ans<br>";
}
}
echo "<br> welcome to Bank <br>";
$c1=new SI(); static function can access
$c1->set(200000,3);
$c1->cal(); only static
$c2=new SI();
$c2->set(800000,4); properties(variables)
$c2->cal();
echo "<br> Thank You <br>";
?>
<?php
class SI
{
public static $r=8.5; //static property
public static function show()
{echo "From Self class ROI = ".SI::$r;
}
}
class x extends SI
{
• static function can be accessed
public function disp() inside class using –
{ echo "<br> From Derived value of ROI :";
echo parent::$r;
classname::$varname
} or
}
echo "<br> welcome to Bank <br>";
self ::$varname
SI::show(); • From derived class ,static
$o=new x();
$o->disp();
variable can be accessed using-
echo "<br> Thank You <br>"; classname ::$varname
?>
or
parent ::$varname
clone :

- The clone keyword is used to create a copy of an object.


<?php
class cltest {
public $var;
}
$a = new cltest;
$a->var = 5;
echo $a->var."<br>";
$b = $a; // assignment
$b->var = 10;
echo $a->var."<br>"; means that any
echo $b->var; modification in $b will also
?> apply to $a
<?php
class cltest {
public $var;
}
$a = new cltest;
$a->var = 5;
echo $a->var."<br>";
$b = clone $a; // cloning of object
$b->var = 10;
echo $a->var."<br>";
means that any
echo $b->var;
modification in $b will
?>
not be applied to $a
<?php
class m
{
public $cnt = 0;
}
$a = new m;
$b = clone $a;
$c = clone $a;
echo $a->cnt."<br>";
echo $b->cnt."<br>";
echo $c->cnt."<br>";
$c->cnt=5; Only cnt value of
echo "After changing value of variable :<br>"; object c is changed.
echo $c->cnt."<br>";
echo $a->cnt."<br>"; Rest clone copies of
echo $b->cnt."<br>"; object a and b are not
?>
affected
<?php
class m
{ public $cnt;
public function __construct() //constrcutor
{ $this->cnt=0;
}
public function __clone() //clone special function
{ $this->cnt++;
}
}
$a=new m();
echo "Original Value :<br>";
echo "Value of cnt of Object a: ".$a->cnt."<br>";
echo "After Cloning for first time Value :<br>";
$b=clone $a; //will call clone()
echo "Value of cnt of Object b: ".$b->cnt."<br>";
echo "Value of cnt of Object a: ".$a->cnt."<br>";
echo "After Cloning for second time Value :<br>";
$c=clone $a; //will call clone()
echo "Value of cnt of Object a: ".$a->cnt."<br>";
echo "Value of cnt of Object b: ".$b->cnt."<br>";
echo "Value of cnt of Object c: ".$c->cnt."<br>";
?>
Introspection in PHP:
- offers the useful ability to examine an object's characteristics, such as
its name, parent class (if any) properties, classes, interfaces, and
methods.
- offers a large number of functions that you can use to accomplish the
task.
In-built functions in PHP Introspection :

Function Description
class_exists() Checks whether a class has been defined.
get_class() Returns the class name of an object.
get parent_class() Returns the class name of a Return object's parent class.

is_subclass_of() Checks whether an object has a given parent class.

get_declared_classes() Returns a list of all declared classes.

get_class_methods() Returns the names of the class methods.

get_class_vars() Returns the default properties of a class

interface_exists() Checks whether the interface is defined.

method_exists() Checks whether an object defines a method.


<?php
class A
{
public function dispA()
{
echo "I am a super class <br>";
}
}
class B extends A
{
public function dispB()
{
echo "welcome";
}
}
$b=new B();
echo "Class name : ".get_class($b)."<br>";
echo "Parent Class name : ".get_parent_class($b);
?>
<?php
class A
{
public function dispA()
{ echo "I am a super class <br>";
}
}
class B extends A
{
public function dispB()
{ echo "welcome";
}
}
if (class_exists("B"))
{ $b = new B();
$b->dispB();
}
?>
<?php
class A
{ public function dispA() {
echo "I am a super class <br>";
}
}
class B extends A
{ public function dispB() {
echo "welcome<br>";
}
}
if (class_exists("B")) {
$b = new B();
$b->dispB();
}
if (is_subclass_of($b, "A"))
{
echo "Yes, " . get_class($b) . " is a subclass of A";
}
else
{
echo "No, " . get_class($b) . " is not a subclass of A";
}
?>
<?php
class A
{
public function dispA() {
echo "I am a super class <br>";
}
}
class B extends A
{
public function dispB() {
echo "welcome<br>";

}
}
$c = get_declared_classes();
echo "The following classes are available:<br>";
print_r($c);
?>
<?php
class A
{ public $x;
public function dispA() { echo "I am a super class <br>"; }
}
class B extends A
{ public $n;
public function dispB() { echo "welcome<br>"; }
}
$c = get_declared_classes();
if (in_array("B", $c))
print "Class A is available <br>";
$b = new B();
$m = get_class_methods($b);
echo "The following methods are available:<br>";
print_r($m);

$vars = get_class_vars("B");
echo "<br>The following properties are available:<br>";
print_r($vars);
?>
Serialization in PHP
- Serialization is a technique used by programmers to preserve their working data in a format
that can later be restored to its previous form.
- Serializing an object means converting it to a byte stream representation that can be stored in
a file.
- Serialization in PHP is mostly automatic, it requires little extra work from you, beyond calling
the serialize() and unserialize() functions.

Serialize():
- The serialize() converts a storable representation of a value.
- The serialize() function accepts a single parameter which is the data we want to serialize and
returns a serialized string.
- A serialize data means a sequence of bits so that it can be stored in a file, a memory buffer, or
transmitted across a network connection link.
It is useful for storing or passing PHP values around without losing their type and structure.
Syntax:
serialize(value);
unserialize():
unserialize() can use string to recreate the original variable values i.e. converts actual data
from serialized data.
Syntax: unserialize(string);
Example:
<?php
$a=array('Shivam','Rahul','Vilas');
$s=serialize($a);
print_r($s);
$s1=unserialize($s);
echo "<br>";
print_r($s1);
?>
Output:

a:3:{i:0;s:6:"Shivam";i:1;s:5:"Rahul";i:2;s:5:"Vilas";}
Array ( [0] => Shivam [1] => Rahul [2] => Vilas )
<?php
$a="Priti";
$s=serialize($a);
print ($s);
$s1=unserialize($s);
echo "<br>";
print ($s1); For String
?> variable
<?php
$a=100;
$s=serialize($a);
print ($s);
$s1=unserialize($s);
echo "<br>";
print ($s1); For Numbers
?>
THANK YOU

You might also like