开发者

How to serialize an SplFileObject with PHP 5.3?

开发者 https://www.devze.com 2023-02-17 14:34 出处:网络
I\'m trying to serialize a SplFileObject, which fails silently in 5.2 and throws a fatal in 5.3. So far, I\'ve fixed the problem in 5.2 by subclassing SplFileObject and implementing the Serializable i

I'm trying to serialize a SplFileObject, which fails silently in 5.2 and throws a fatal in 5.3. So far, I've fixed the problem in 5.2 by subclassing SplFileObject and implementing the Serializable interface using the (simplified) following code (full version also handles $open_mode and $context but that's not relevant to that question):

<?php

class SerializableFileObject extends SplFileObject implements Serializable
{
  public开发者_JS百科 function serialize()
  {
    return $this->getRealPath();
  }

  public function unserialize($serialized)
  {
    $this->__construct($serialized);
  }
}

but 5.3 still throws a fatal:

PHP Fatal error:  Class SerializableFileObject could not implement interface Serializable in Unknown on line 0

I also tried adding magic __sleep and __wakeup methods, to no avail.

Google doesn't seem to know much about that, so I'm left wondering if it's even possible to serialize an SplFileObject in 5.3.

UPDATE: seems like that question doesn't have an answer (cf my comment).


Apparently you're not allowed to serialize it. Tested in cli mode and this is what I've got:

Warning: Uncaught exception 'Exception' with message 'Serialization of 'SplFileObject' is not allowed' in php shell code:1

Not much wandering considering that resources (mysql connection, file handlers) cannot be serialized, and SplFileObject is all based around a file resource.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号