Francisco M. Marzoa Alonso wrote:

Hi,

I'm trying to wrote my own serialization routines and I've found a previsible problem: protected members are not visible to my serialization routine. This is ok and it should be as is, but I've seen that PHP's serialize function have access to that members anyway, so the question is: Is there any kind of hack that I can use to access those variables from my own serialization routine?

Thx. a lot in advance,


An easy hack to get private / protected members:

class read_only_hack {

  // overload the get "magic" function
  function __get($var) {
    return $this->$var;
  }
}


Another way that should work (untested):

class serialize_me {
  function __sleep() {
    $copy = clone($this);
    // inside your class, so you can access private and push onto array
    $copy->_private_vars = array();
    $copy->_protected_vars = array();
    return $copy;
  }

  // object being unserialized, so now you assign the properties
  function __wakeup() {
    foreach ($this->_protected_vars as $key=>$val) {
      $this->$key = $val;
    }
    foreach ($this->_private_vars as $key=>$val) {
      $this->$key = $val;
    }
    unset ($this->_private_vars, $this->_protected_vars);
  }

}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to