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

»

5 Things to Consider When Designing A Logo For Your Startup

Designing a Logo is essential because it communicates your business. ...

»

What Is Access Control?

Access Control, When running a business, there are dozens of things ...

»

Process Management Solution For a Business

In order to be successful, a company needs to be on the same page and ...

»

5 Metrics You Should Track on Every Blog Post

As a blogger, you may have developed your blog with a variety of blog ...

»

3 of the Most Dynamic LMS Plugins for WordPress

The possibilities are limitless when you want to put a course online ...

»

Do You Really Need Access to Advanced Scripting Capabilities?

One of the features you’ll find most commonly advertised when ...

»

Obvious UX Trends that will Rule 2018

For web design industry, we see new UX trends in the way we build ...

»

Java Programming Language And Its Application

Programming language is one of the main IT developments of the past ...

»

Why Can’t I Add or Install Plugins in WordPress

WordPress is a great choice when it comes to Content Management ...