我可以在PHP 5.3中恢复旧的__tostring()行为吗? [英] Can I bring back old __tostring() behaviour in PHP 5.3?
问题描述
我必须将一个网站(自定义编写(不是我本人),因此不能选择仅更新CMS)移至PHP 5.3服务器.该代码抱怨::
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
我在Google上搜索发现问题是因为在PHP 5.3中,魔术方法__tostring()不再接受任何参数","...通过接受参数实现了__tostring()...现在不赞成使用PHP 5.3的新__tostring(). 这是代码:
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);
}
是否有类似php.ini参数的内容,我可以进行调整以使其恢复工作?
Is there something like a php.ini parameter I can tweak to bring this back to work?
我不是PHP开发人员,也不是我很愿意研究和修改由某些Web开发外包公司在我工作的公司之前编写的代码.我的任务是将网站从共享主机提供商移到我管理的专用服务器(我在那里运行Ubuntu 10.04 LTS Server),并且我强烈不希望将其降级到PHP 5.2.如果我能使其与某种配置魔术配合使用,那将是很好的.恐怕如果我修改方法,那么整个过程将无法按预期工作.
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.
推荐答案
您不能使__toString()再次接受参数.方法签名必须很浅. (我同意这是不必要的弃用,为什么他们不只是抛出E_DEPRECATED而不是导致致命的不兼容,这也不是很明智.)
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.)
唯一的解决方法是使用func_get_args()
代替实际参数:
The only workaround is utilizing func_get_args()
in lieu of real parameters:
class EmptyString {
function __toString() {
print_r(func_get_args());
return "";
}
}
这将使5.3调用"$so";
的隐式__toString,但仍允许使用手册$so->__toString(_WITH, "params");
.
That will make 5.3 call the implicit __toString for "$so";
, but still allow a manual $so->__toString(_WITH, "params");
.
这种方案当然不能简化参数默认值.而且,由于您的目标是使旧版应用程序正常运行,因此这还不够.您需要在基类中实现此变通方法,并将现有的__toString方法改编为__oldString并在兼容模式下调用该方法.无法进行任何重写.
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);
}
这篇关于我可以在PHP 5.3中恢复旧的__tostring()行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!