Results 1 to 6 of 6

Thread: [RESOLVED] object serialisation

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Resolved [RESOLVED] object serialisation

    When an object is serialised using the serialize() function, does that include objects stored by reference as member variables in the original object? If not, will they be serialised if stored as explicit copies? Or is there no way to serialise the whole thing at once?

    Cheers
    - P

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: object serialisation

    Yes and yes. PHP does howeverkeep track of specific object instances so if you have recursive object refernece, PHP will not go on an serialise forever. Also, if you serailise several objects which contain a reference to a common object (i.e. an owner), then the common object will only be serialised and sotred once.

    There is a solution if you do not want to preserve the objects entire state. These are the __sleep() and __wakeup() magic functions. Use __sleep() to return an array of properties to serialise and __wakeup() to restablish the vlaues of any other properties when unserialising the object.
    PHP Code:
    class Serializable
    {
        var 
    $db;
        var 
    $property1;
        var 
    $property2;

        function 
    Serializable($p1$p2)
        {
            
    $this->property1 $p1;
            
    $this->proeprty2 $p2;

            
    $this->db = new MySqlDB('blah''blah''blah');
        }
        
        function 
    __sleep()
        {
            return array(
    'property1''property2');
        }

        function 
    __wake()
        {
            
    $this->db = new MySqlDB('blah''blah''blah');
        }

    Obviously, in PHP 5, you'll need to declare these functions a public. However,the properties do not need to be public. I also found in PHP 5, that if the class you are serialising is a descendant, only the __sleep() and __wakeup() functions for the descendant class are called. This means that if you want to preserve any properties in the parent class, you will have to ensure its properties are either declared public or protected and add them to the array in the __sleep function.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: object serialisation

    Sweet. Thanks

    Another related question. If I store an object reference in a session variable, would that preserve its referenced objects? I wasn't sure which is why I was looking into serialising it instead.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: object serialisation

    If you look at the session files gnereated, you'll see that the variables stored in $_SESSION are serialised, hence, the __sleep() and __wakeup() functions are called and all that I said above applies
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: object serialisation

    Thanks!

    Will spread happiness and joy in return.

    Edit: In due course, as is deemed necessary.

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: object serialisation

    Feel free to rate me negatively if you find serialisation offensive .
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width