我可以在 PHP 5.3 中恢复旧的 __tostring() 行为吗?

发布于 2024-10-21 08:58:52 字数 777 浏览 2 评论 0原文

我必须将一个网站(自定义编写(不是由我编写),因此不能只更新 CMS)迁移到 PHP 5.3 服务器。代码抱怨::


Fatal error: Method cDate::__tostring() cannot take arguments in ...\lib.datetime.php on line 183

我用谷歌搜索发现问题是因为“在 PHP 5.3 中,魔术方法 __tostring() 不再接受任何参数”,“...通过接受参数来实现其 __tostring() ...现已弃用,取而代之的是 PHP 5.3 的新 __tostring()”。 这是代码:


public function __toString($format = 'Y-m-d')
{
  // the next is the line #182, the line #183 mentioned in the error message is the closing brace
  return date($format, $this->_stamp);
}

是否有类似 php.ini 参数的东西我可以调整以使其恢复工作?

我不是一个PHP开发人员,我也不太愿意去研究和修改我工作的公司过去由一些Web开发外包公司编写的代码。我的任务是将网站从共享托管提供商移动到我管理的专用服务器(我在那里运行 Ubuntu 10.04 LTS 服务器),并且我强烈不希望将其降级到 PHP 5.2。如果我能通过一些配置魔法让它工作,那就太好了。我担心如果我修改一个方法,那么整个事情就会按预期停止工作。

I've got to move a web site (custom-written (not by me), so just updating a CMS is not an option) to a PHP 5.3 server. The code complains::


Fatal error: Method cDate::__tostring() cannot take arguments in ...\lib.datetime.php on line 183

I've googled to find out that the problem is because "with PHP 5.3 the magic method __tostring() no more accepts any parameter", "... implements its __tostring() by accepting parameter ... which is now deprecated in favor of the new __tostring() for PHP 5.3".
Here's the code:


public function __toString($format = 'Y-m-d')
{
  // the next is the line #182, the line #183 mentioned in the error message is the closing brace
  return date($format, $this->_stamp);
}

Is there something like a php.ini parameter I can tweak to bring this back to work?

I am not a PHP developer, neither I am too much willing to dive into studying and modifying the code which was written by some web dev outsourcing company in the past of the company I work for. My task is to move the web site from a shared hosting provider to a dedicated server which I admin (I run Ubuntu 10.04 LTS Server there) and which I'd strongly prefer not to downgrade to PHP 5.2. It would be great If I could just make it work with some configuration magic. I am afraid that if I modify a method, then the entire thing is going to stop working as expected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

浪荡不羁 2024-10-28 08:58:52

您不能让 __toString() 再次接受参数。方法签名需要很浅。 (我同意这是一个不必要的弃用,并且为什么他们不直接抛出 E_DEPRECATED 而不是生成致命的不兼容性并不是很明智。)

唯一的解决方法是利用 func_get_args() 代替 real参数:

class EmptyString { 
    function __toString() {
        print_r(func_get_args());
        return "";
    }
}

这将使 5.3 为 "$so"; 调用隐式 __toString,但仍然允许手动 $so->__toString(_WITH, "params");

当然,该方案不允许简单的参数默认值。由于您的目标是让旧版应用程序正常运行,因此这是不够的。您需要在基类中实现此解决方法,并将现有的 __toString 方法改编为 __oldString 并在兼容模式下调用它。没有办法绕过一点重写。


没有任何复杂的包装方法,您的具体示例代码将进行调整:

public function __toString()
{
    $format = func_get_arg(0)   or   $format = 'Y-m-d';

    return date($format, $this->_stamp);
}

You cannot make __toString() accept parameters again. The method signature needs to be shallow. (I agree that is a needless deprecation, and why they didn't just throw an E_DEPRECATED instead of generating a fatal incompatibility is not very sensible.)

The only workaround is utilizing func_get_args() in lieu of real parameters:

class EmptyString { 
    function __toString() {
        print_r(func_get_args());
        return "";
    }
}

That will make 5.3 call the implicit __toString for "$so";, but still allow a manual $so->__toString(_WITH, "params");.

This scheme does not allow for the simplicity of parameter defaults of course. And since your goal is to get a legacy app working, it is insufficient. You need to implement this workaround in a base class, and adapt existing __toString methods into __oldString and have that invoked in compat mode. There is no way around a bit of rewriting.


Without any complicated wrapper methods, your specific example code adapted:

public function __toString()
{
    $format = func_get_arg(0)   or   $format = 'Y-m-d';

    return date($format, $this->_stamp);
}
梦里的微风 2024-10-28 08:58:52

如果问题仅出现在此类中,您可以更新该类的代码。

创建一个重写方法 __toString(),它接受参数,该调用是父函数或完全重写该方法的代码(我认为这并不难)。

但是为了获得更多信息,我们需要一些代码来在上下文中解决问题。

编辑:好的,我认为更好的解决方案是:

  1. 重命名该方法及其所有调用。因为,__toString 被保留给 magick 方法:printDate($fomat='Ymd')
  2. 添加一个新方法 __toString() 来调用 printDate()

并使用:

public function __toString()
{
    $format = isset(func_get_arg(0) ? func_get_arg(0) : 'Y-m-d';
    return date($format, $this->_stamp);
}

If the problem is only in this class you can update the code of that class.

Create an override method __toString(), which accept parameters, that call is parent or completely rewrite the code of the method (I think it's not so hard).

But for more informations, we need some portion of code to sse the problem in context.

EDIT: Ok, I think the better solution is:

  1. rename the method and all his call. Because, the __toString is reserved to the magick method: printDate($fomat='Y-m-d').
  2. add an new method __toString() that call your printDate()

And use:

public function __toString()
{
    $format = isset(func_get_arg(0) ? func_get_arg(0) : 'Y-m-d';
    return date($format, $this->_stamp);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文