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.
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: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: