如何使用 PHP 5.3 序列化 SplFileObject?
我正在尝试序列化一个 SplFileObject,它在 5.2 中默默失败,并在 5.3 中引发致命错误。到目前为止,我已经通过子类化 SplFileObject 并使用(简化的)以下代码实现 Serialized 接口解决了 5.2 中的问题(完整版本还处理 $open_mode 和 $context 但这与该问题无关):
<?php
class SerializableFileObject extends SplFileObject implements Serializable
{
public function serialize()
{
return $this->getRealPath();
}
public function unserialize($serialized)
{
$this->__construct($serialized);
}
}
但 5.3 仍然抛出fatal:
PHP Fatal error: Class SerializableFileObject could not implement interface Serializable in Unknown on line 0
我还尝试添加魔术 __sleep
和 __wakeup
方法,但无济于事。
Google 似乎对此了解不多,所以我想知道是否可以在 5.3 中序列化 SplFileObject。
更新:似乎这个问题没有答案(参见我的评论)。
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 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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然你不被允许序列化它。在 cli 模式下进行了测试,这就是我得到的结果:
考虑到资源(mysql 连接、文件处理程序)无法序列化,并且
SplFileObject
全部基于文件资源,因此没有太多徘徊。Apparently you're not allowed to serialize it. Tested in cli mode and this is what I've got:
Not much wandering considering that resources (mysql connection, file handlers) cannot be serialized, and
SplFileObject
is all based around a file resource.