
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Serializable interface
Introduction
The Serializable interface is present in PHP library to build a class that provides custimised serialzing. PHP's serialize() function is able to serialize most of the values to a storable representation. However, objects of user defined classes can not be serialized. This interface makes it possible.
Syntax
Serializable { /* Methods */ abstract public serialize ( void ) : string abstract public unserialize ( string $serialized ) : void }
Methods
Serializable::serialize — String representation of object
Serializable::unserialize — Constructs the object from serialized string representation
The built-in serialze() function Generates a storable representation of a value
serialize ( mixed $value ) : string
unserialize() function Creates a PHP value from a stored representation
unserialize ( string $str [, array $options ] ) : mixed
Serializable Example
In following example, a string variable is used private proprty of myclass. When built-in serialize() function uses object of this class as argument, serialize() method is automatically called. Similarly, unserialize() function reconstructs the object with string prvate property.
Example
<?php class myclass implements Serializable { private $arr; public function __construct() { $this->arr = "TutorialsPoint India (p) Ltd"; } public function serialize() { echo "Serializing object..
"; return serialize($this->arr); } public function unserialize($data) { echo "Unserializing object..
"; $this->arr = unserialize($data); } public function getdata() { return $this->arr; } } $obj = new myclass; $serobj = serialize($obj); var_dump ($serobj); $obj1 = unserialize($serobj); var_dump($obj1->getdata()); ?>
Output
Above program shows following output
Serializing object.. string(55) "C:7:"myclass":36:{s:28:"TutorialsPoint India (p) Ltd";}" Unserializing object.. string(28) "TutorialsPoint India (p) Ltd"
- Related Articles
- Serializable Interface in Java
- PHP ArrayAcccess interface
- PHP Iterable interface
- PHP IteratorAggregate interface
- PHP Throwable interface
- PHP Traversable interface
- Explain interface in PHP.
- What is Stringable Interface in PHP 8?
- Difference between Parcelable and Serializable in android
- Difference between Parcel able and Serializable in android
- How can I make my custom objects Serializable?
- How to pass data between activities with android Serializable
- Check if the given schedules are view serializable(DBMS)
- What is the difference between Externalizable and Serializable interfaces in Java?
- Why an interface cannot implement another interface in Java?
