Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: OOP issue

  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Posts
    120
    Thanks
    13
    Thanked 3 Times in 3 Posts

    OOP issue

    Is it possible to register a session var with the content value of a whole object and its properties, simply using the $this var?

    Example:

    PHP Code:
    class myclass(){

    public function 
    registerSession(){

     
    $_SESSION['myobject']=$this//$this refers to the whole object

    }


  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Yes, however you will need to save the object by first serializing the object, and then unserializing the object when the session begins again

    serialize() | unserialize()
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Not true, PHP will automatically serialize the object. If overridden, it will call the __sleep and __wakeup methods prior to the serialize and unserialize methods. These can be useful (I use the term fairly lightly) if you wanted to store a class that connects to an external resource. Since the resource itself cannot be serialized, you can sleep with the configurations necessary and wakeup with recalling a connection handler. The automatic unserialize call will repopulate any variables required for configurations that were stored when the sleep was called.

    So to answer the OP's question, yes you can do just as you have done above. Since you're working in a PHP5 environment, you need not concern yourself with reference handling on $this.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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