0% found this document useful (0 votes)
8 views

php exp9 23

The document outlines a laboratory experiment for a web page development course using PHP, focusing on introspection and serialization. It includes a PHP program demonstrating class inheritance and serialization methods, as well as practical questions regarding the unserialize() function and magic methods related to object serialization. Additionally, it provides an exercise to declare a class with user-defined functions and write a script based on unserialize().

Uploaded by

aafu202
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

php exp9 23

The document outlines a laboratory experiment for a web page development course using PHP, focusing on introspection and serialization. It includes a PHP program demonstrating class inheritance and serialization methods, as well as practical questions regarding the unserialize() function and magic methods related to object serialization. Additionally, it provides an exercise to declare a class with user-defined functions and write a script based on unserialize().

Uploaded by

aafu202
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF COMPUTER ENGINEERING

Subject: web page development using php Subject Code: 22619


Semester: 6 Course: CO6I-A
Laboratory No: Name of Subject Teacher: Saqib Ghatte
Name of Student: AFIF IRFAN NAZIR Roll Id: 21203A1005

Experiment No: 9
Title of Experiment Write a simple PHP program on Introspection and Serialization.

Program code:
<?php
class Derived
{
public function details()
{
echo "I am a Derived(super) class for the Child(sub) class. <BR>";
}
}
class sub extends Derived
{
public function details()
{
echo "I'm " .get_class($this) , " class.<BR>";
echo "I'm " .get_parent_class($this) , "'s child.<BR>";
}
}
//details of parent class
if (class_exists("Derived"))
{
$der = new Derived();
echo "The class name is: " .get_class($der) . "<BR>";
$der->details();
}
//details of child class
if (class_exists("sub"))
{
$s = new sub();
$s->details();
if (is_subclass_of($s, "Derived"))
{
echo "Yes, " .get_class($s) . " is a subclass of Derived.<BR>";
}
else
{
echo "No, " .get_class($s) . " is not a subclass of Derived.<BR>";
}
}
Output :
The class name is: Derived
I am a Derived (super) class for the Child(sub) class.
I'm sub class.
I'm Derived's child.
Yes, sub is a subclass of Derived

Practical related questions

1. State the use of unserialize().


unserialize() is a PHP function that is used to convert a string representation of a serialized
data into its corresponding PHP value. Serialized data is a way of representing complex data
structures such as arrays and objects as a string that can be easily stored or transmitted.

2. List the Magic Methods with PHP Object Serialization


n PHP, there are several magic methods that can be used to control the behavior of object
serialization, which is the process of converting a PHP object into a format that can be stored
or transmitted. Here are some of the key magic methods related to object serialization:

__sleep(): This method is called before an object is serialized, and allows you to specify
which properties should be included in the serialized representation of the object. If this
method is not defined, all properties will be serialized by default.

__wakeup(): This method is called after an object has been unserialized, and allows you to
perform any necessary post-processing on the object.

__serialize(): This method is called before an object is serialized, and allows you to
customize the serialization process. You can use this method to define custom serialization
logic, or to modify the data that will be serialized.
__unserialize(): This method is called after an object has been unserialized, and allows you to
customize the unserialization process. You can use this method to perform any necessary
post-processing on the data that has been unserialized.

__toString(): This method is called when an object is used in a string context, such as when it
is concatenated with another string. It allows you to define how the object should be
represented as a string.

__set_state(): This method is used by var_export() to create a string representation of an


object that can be used to recreate the object later. You can use this method to customize how
the object is created from the exported string.

By implementing these magic methods in your PHP classes, you can customize the behavior
of object serialization and make your code more flexible and robust.

Exercise
1. Declare a class as “Test” with three user defined functions. List name of the class and
functions declared in the class “Test”.
class Test {
public function func1() {
// Function logic here
}

public function func2() {


// Function logic here
}

public function func3() {


// Function logic here
}
}
n this example, the Test class has three functions: func1(), func2(), and func3(). These
functions are defined as public methods of the class, which means that they can be called
from outside the class.

2. Write a script to based on unserialize()

You might also like