问题
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.
回答1:
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);
}
回答2:
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:
- rename the method and all his call. Because, the __toString is reserved to the magick method:
printDate($fomat='Y-m-d')
. - 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);
}
来源:https://stackoverflow.com/questions/5260607/can-i-bring-back-old-tostring-behaviour-in-php-5-3