How to set $this in PHP5

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chung Leong

    How to set $this in PHP5

    I ran into this problem trying to run a PHP4 program in PHP5:

    A class implements persistence by serializing itself and saving the string
    to a file. In the constructor, the object restores itself by setting $this
    to the result from unserialize(). In PHP4, this is legal. In PHP5 I get an
    error. How do I fix this aside from manually copying the object properties?


  • Janwillem Borleffs

    #2
    Re: How to set $this in PHP5

    Chung Leong wrote:[color=blue]
    > I ran into this problem trying to run a PHP4 program in PHP5:
    >
    > A class implements persistence by serializing itself and saving the
    > string to a file. In the constructor, the object restores itself by
    > setting $this to the result from unserialize(). In PHP4, this is
    > legal. In PHP5 I get an error. How do I fix this aside from manually
    > copying the object properties?[/color]

    If you don't want to copy the properties, the only thing you can do is
    storing the unserialized object as a class property and use it from there.

    Otherwise, you could use the reflectionObjec t class to simplify the task of
    copying the properties:

    $ro = new reflectionObjec t($obj);
    foreach ($ro->getProperties( ) as $propObj) {
    $this->{$propObj->name} = $obj->{$propObj->name};
    }


    JW



    Comment

    Working...