InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Controlling Object Serialization in PHP
PHP

Controlling Object Serialization in PHP

InfinityCoder November 25, 2016

You want to control how an object behaves when you serialize() and unserialize() it. This is useful when you need to establish and close connections to remote resources, such as databases, files, and web services.

Define the magical methods __sleep() and __wakeUp():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class LogFile {
   protected $filename;
   protected $handle;
 
   public function __construct($filename) {
      $this->filename = $filename;
      $this->open();
   }
   private function open() {
      $this->handle = fopen($this->filename, 'a');
   }
 
   public function __destruct($filename) {
      fclose($this->handle);
   }
 
   // called when object is serialized
   // should return an array of object properties to serialize
   public function __sleep() {
      return array('filename');
   }
 
   // called when object is unserialized
   public function __wakeUp() {
      $this->open();
   }
}

When you serialize an object in PHP, it preserves all your object properties. However, this does not include connections or handles that you hold to outside resources, such as databases, files, and web services.
These must be reestablished when you unserialize the object, or the object will not behave correctly. You can do this explicitly within your code, but it’s better to abstract this away and let PHP handle everything behind the scenes.
Do this through the __sleep() and __wakeUp() magic methods. When you call seri alize() on a object, PHP invokes __sleep(); when you unserialize() it, it calls __wakeUp().
The LogFile class in the Solution has five simple methods. The constructor takes a filename and saves it for future access.

The open() method opens this file and stores  the file handle, which is closed in the object’s destructor.
The __sleep() method returns an array of properties to store during object serialization.
Because file handles aren’t preserved across serializations, it only returns array(‘filename’) because that’s all you need to store.
That’s why when the object is reserialized, you need to reopen the file. This is handled inside of __wakeUp(), which calls the same open() method used by the constructor.
Because you cannot pass arguments to __wakeUp(), it needs to get the filename fromsomewhere else.

Fortunately, it’s able to access object properties, which is why the filename is saved there.
It’s important to realize that the same instance can be serialized multiple times in a single request, or even continue to be used after it’s serialized.

Therefore, you shouldn’t do anything in __sleep() that could prevent either of these two actions.

The __sleep() method should only be used to exclude properties that shouldn’t be serialized because they take up too much disk space, or are calculated based on other data and should be recalculated or otherwise made fresh during object unserialization.
That’s why the call to fclose() appears in the destructor and not in __sleep().

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Extracting Information Using XPath in PHP
You want to make sophisticated queries of your XML data …

Extracting Information Using XPath in PHP

Manipulating UTF-8 Text PHP
You want to work with UTF-8–encoded text in your programs. …

Manipulating UTF-8 Text PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap