PHP Magic Functions: Best Part of Object Oriented PHP – Final Part

Continuing my posts on PHP Magic Functions lets close this topic now and talk about rest of the Magic Functions.

__sleep and __wakeup : Serialize and Unserialize Your Objects as You Wish

Before talking about these magic functions, I want you to be clear about serialization and unserialization of a variable. Some times you need to ( may be you’ve never done so but now you’ll atleast think about it ) store ( using string representation ) your variable to database or primary storage or session or any where and from that location you can get the same value again.

To acheive this you can use either of two available possiblilities in PHP. First one is use of serialize and unserialize functions and another one is use of json_encode and json_decode functions. I personally prefer the formar approach since serialize and unserialize functions are quite faster than json_encode and json_decode functions. The latter approach is useful when you need to port your data from php to other programming languages like javascript or actionscript etc.

Have take a look, what I’m talking about …

<?php
	/**
	 * Case I - Using serialize and unserialize
	 */
	// For generating string representation of data.
	$stringRepresentation = serialize( $myVar );
	// For regenerating the same data again.
	$myVar = unserialize( $stringRepresentation );
 
	/**
	 * Case II - Using json_encode and json_decode
	 */
	// For generating string representation of data.
	$stringRepresentation = json_encode( $myVar );
	// For regenerating the same data again.
	$myVar = json_decode( $stringRepresentation );

If you call serialize function on an object then it checks first whether there you’ve written __sleep magic function or not in your class. If you’ve then that function gets executed and the return value of the function is serialized or the object itself gets serialized. Now think where it can be useful. Suppose you’ve a class that connects you to the mysql database and stores basic information required for that ( host, user name, password ) then you’ll never wish to serialize your object along with your secret informations ( password in this case ). In that case you can employee the __sleep magic function and choose which values you want to export from the object. Lets see how I’ll code my database connect class …

<?php
	class ConnectDb {
		private $_hostName = 'localhost';
		private $_userName = 'mysqluser';
		private $_password = 'mysqluserpassword';
		private $_selectedDb = 'my_blog_db';
 
		public function connect() {
			// code for connecting to database
		}
 
		function __sleep() {
			return array( '_hostName', '_userName', '_selectedDb' ); // Export just these three data members, leaving password
		}
	}
 
	$connectToDb = new ConnectDb();
	$connectToDb->connect();
	var_dump( serialize( $connectToDb ) );

The above snippet of code will output something like this …

string(156) "O:9:"ConnectDb":3:{s:20:"ConnectDb_hostName";s:9:"localhost";s:20:"ConnectDb_userName";s:9:"mysqluser";s:22:"ConnectDb_selectedDb";s:10:"my_blog_db";}"

Recently Published

»

Know-How DevOps Benefits Your Business

The organizations in this current fast-paced environment need to ...

»

10 Best Prototyping Tools for UI/UX Designers

Website design for a client gets only successful if you follow their ...

»

Social Media Marketing: Strategy To Boost Ecommerce-Traffic

According to a recent study, the experts revealed that people aged ...

»

How Flutter Can Help Reduce Overall Mobile App Development Cost?

Before jumping on answering the question, let me first walk you ...

»

Which One is Better? DigitalOcean Or AWS?

Digitalocean or AWS: Picking a cloud service for your application or ...

»

Killing way to select Domain Name and Hosting

Planning to launch your online presence? We know it’s all very ...

»

7+ blogs to inspire how they made more than $200 in a day

On this page I want to go through the blogs on how they earn money ...

»

5 Best Practices to Make Responsive Web Design in 2020

According to statistics, 40.1% of the time spent on websites came ...

»

7 sites to download free stock videos for TikTok or YouTube

Many of us always wandering for free stock videos to integrate into ...