আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

PHP Object Oriented Programming Part-11: PHP Object Serializing

What is Object Serializing and unserializing in PHP OOP?

When developing various PHP based projects, to pass the objects through web forms, urls, or to save to a text file and database, Many times our objects need to be converted to a string. The method to change the object to a string in PHP OOP is called Object Serializing. And the method to change the string to the object is called Object Unserializing.

How to convert object to string or serializing in PHP OOP?

Object is converted into a string by serialize() function. Let’s explain with an example:

<?php
class Member
{
  public $username = "";
  private $loggedIn = false;
   
  public function login() {
    $this->loggedIn = true;
  }
   
  public function logout() {
    $this->loggedIn = false;
  }
   
  public function isLoggedIn() {
    return $this->loggedIn;
  }
}
  
$member = new Member();
$member->username = "Farhan";
$member->login();
  
$memberString = serialize($member);
echo "Converted the Member to a string: $memberString \n";
?>

Explanation: Notice, We have created a common member class, a $username named public and $loggedIn named private property and three public functions respectively login(), logout() and isLoggedIn(). Our script then creates a new object, set a user named “Farhan” to the $username property and made the user “Farhan” log in to with Login Method.

Then we call the serialize() function and pass the Member object to the serialize() function. serialize() function returns a string of the object, Which we save to $memberString and show it on page.

How to convert String into Object or Unserializing into PHP OOP?

String can be transformed into an object with unserialize() function in PHP. Let’s explain with an example:

<?php
class Member
{
  public $username = "";
  private $loggedIn = false;
   
  public function login() {
    $this->loggedIn = true;
  }
   
  public function logout() {
    $this->loggedIn = false;
  }
   
  public function isLoggedIn() {
    return $this->loggedIn;
  }
}
  
$member = new Member();
$member->username = "Farhan";
$member->login();
  
$memberString = serialize( $member );
$member2 = unserialize( $memberString );
echo $member2->username . " is " . ( $member2->isLoggedIn() ? "logged in" : "logged out" ) . "<br>";

Explanation:Notice, we have convert the string created by serialize() function to object by a unserialize function and keep that in a variable named $member2. Now we use $member2 as Object variable.

What is the way to convert or serialize to a string with some properties instead of all the properties of a big object?

If you want to convert or serialize to a string with certain properties instead of all the properties of a large object, then use the __sleep() magic method in PHP. Let’s understand with two examples:

serialize without __sleep () method:

<?php
class Member{
  public $Name="Masud Alam";
  public $Email="[email protected]";
  public $mobile="01788990099";
  public  $address="Dhaka,Bangladesh";
}
  
$member = new Member();
  
$memberString = serialize( $member );
 
echo $memberString;
?>

Result:

O:6:"Member":4:{s:4:"Name";s:10:"Masud Alam";s:5:"Email";
s:14:"[email protected]";s:6:"mobile";
s:11:"01788990099";s:7:"address";s:16:"Dhaka,Bangladesh";}

Explanation: Notice, Object with all our properties has been converted to serialize string.

serialize with __sleep () method:

<?php
class Member{
  public $Name="Masud Alam";
  public $Email="[email protected]";
  public $mobile="01788990099";
  public  $address="Dhaka,Bangladesh";
  public function __sleep(){
    return ["Name","Email"];
  }
}
  
$member = new Member();
  
$memberString = serialize( $member );
 
echo $memberString;
?>

Explanation: Notice, Among all of our properties only those are serialized in string which are in array in __sleep().

When converting a serialize string to unserialize or Object, can we do Object re-initialization, that’s mean, can we make Database Connection, and do Session Start?

If you want to reestablish or re-initialization the object when unserialize a serialize string or re-initialize the object, then use the __wakeup () magic method in PHP. Let’s understand the matter with an example:

<?php
class Connection
{
    protected $link;
    private $dsn, $username, $password;
     
    public function __construct($dsn, $username, $password)
    {
        $this->dsn = $dsn;
        $this->username = $username;
        $this->password = $password;
        $this->connect();
    }
     
    private function connect()
    {
        $this->link = new PDO($this->dsn, $this->username, $this->password);
    }
     
    public function __sleep()
    {
        return array('dsn', 'username', 'password');
    }
     
    public function __wakeup()
    {
        $this->connect();
    }
}?>

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

Leave a Reply

Your email address will not be published. Required fields are marked *