如何使用 PHP 5.3 序列化 SplFileObject?

发布于 2024-10-23 11:09:13 字数 777 浏览 4 评论 0原文

我正在尝试序列化一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤蝉 2024-10-30 11:09:13

显然你不被允许序列化它。在 cli 模式下进行了测试,这就是我得到的结果:

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

考虑到资源(mysql 连接、文件处理程序)无法序列化,并且 SplFileObject 全部基于文件资源,因此没有太多徘徊。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文