SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Why we use serialize, unserialize in PHP?

    Why we use serialize, unserialize in PHP?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Its a way of getting to some "previously prepared" data.

    Instead of say, making an array from a database select which might be expensive, you just, say, retrieve a cached, serialized array.

    It may not be the best analogy, but think of it a bit like the speed dial on your phone, to phone home you could decide to enter in the 10 digit number - but instead you press "1" and "call".

    You can delete the speed dial, or update it whenever you want.

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Serialize and Unserialize are two of the most handy functions in PHP, but in my eyes, it's best for OOP PHP.

    For example, if you have a class which can hold information of products. You can do this by calling info from a database, and filling in the class information. Then, the user may be able to change information (such as the QTY). The serialized can be stored in the $_SESSION, and you can recall that exact class on every page.

    This means that you can literally store class information in a database, session, text file etc, as a string. then, rebuild it back into a class.

    You can do the same for all PHP variables, most commonly the session array.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can store an object directly in session. PHP will automatically serialize/unserialize it for you.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Off Topic:

    Crikey, thats 2 things I learned from you this morning already.

    RelaxNG and this. Thanks Kyber

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Crikey, thats 2 things I learned from you this morning already.

    RelaxNG and this. Thanks Kyber
    You're welcome.

  7. #7
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are even a few Javascript packages that can decode PHP serialized data. It's usually best to use JSON which is essentially the same thing, but for Javascript. Still, it's an interesting idea if you do a lot RPC AJAX sort of work.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •