SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
-
Sep 21, 2007, 00:50 #1
- Join Date
- Jul 2007
- Posts
- 1
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Why we use serialize, unserialize in PHP?
Why we use serialize, unserialize in PHP?
-
Sep 21, 2007, 01:12 #2
- 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.
-
Sep 21, 2007, 01:21 #3
- 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
-
Sep 21, 2007, 01:35 #4
- 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.
-
Sep 21, 2007, 01:55 #5
- 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
-
Sep 21, 2007, 04:08 #6
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Sep 21, 2007, 11:23 #7
- 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