Can I bring back old __tostring() behaviour in 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

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:

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); }


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:

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

And use:

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


  • how to count object in javascript
  • Is fluent NHibernate ready for production code?
  • When Does The JDK Compile Version Matter?
  • Which files are responsible specifically for Model, View and Controller in struts2 MVC framework? [d
  • Java 6 - Creating and detecting the first double value above Long.MAX_VALUE
  • ZipArchive extraction - single file
  • Value cannot be null.Parameter name: stream error while reading the contents of embeddded file
  • psycopg2 and SQL injection security
  • What does the exclamation mark before class path do in ProGuard?
  • Scala Slick Database Views
  • Change zIndex in HighChart
  • mysql table locked after php crashes
  • How to protect the mp3 file from read or copy on Android?
  • Excel passing a range into a function
  • Nested projects in multiproject visual studio templates
  • Java : How to tint this PNG programmatically?
  • UIAlertController button function not working
  • perl, mysql - fasting way to upload a csv file into mysql?
  • SetWindowsHookEx does not react on media keys
  • Element.tagName for python not working
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • Install PHP intl extension on MacOS
  • Retrieve list of sent friend requests from friend_request FQL table
  • Installing iPhone App to iPhone
  • NHibernate Validation Localization with S#arp Architecture
  • Asynchronous UI Testing in Xcode With Swift
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Why is an OPTIONS request sent to the server?
  • How can I send an e-mail from a vbs script
  • Accessing IRQ description array within a module and displaying action names
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Arrays break string types in Julia
  • Comma separated Values
  • Getting Messege Twice Using IMvxMessenger
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • How to load view controller without button in storyboard?