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 …