Req #46225 [Com]: Allow for optional arguments to __toString() magic method

From: Date: Sat, 14 Mar 2015 10:15:24 +0000
Subject: Req #46225 [Com]: Allow for optional arguments to __toString() magic method
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-191381@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=46225&edit=1 ID: 46225 Comment by: ianbytchek at gmail dot com Reported by: phpbugs at sevenlight dot com Summary: Allow for optional arguments to __toString() magic method Status: Open Type: Feature/Change Request Package: Feature/Change Request Operating System: OSX 10.5.5 PHP Version: 5.3.0alpha2 Block user comment: N Private report: N New Comment: Because there is already a method for that and because this is an extra call. When you are converting thousands of objects into strings this becomes noticeable. Using your example: ``` $iterations = 1000000; $test = new Test(); $this->runBenchmark('toString', function () use ($iterations, $test) { for ($i = 0; $i < $iterations; $i++) { $result = $test->toString(); } }); $this->runBenchmark('__toString', function () use ($iterations, $test) { for ($i = 0; $i < $iterations; $i++) { $result = $test->__toString(); } }); $this->runBenchmark('(string)', function () use ($iterations, $test) { for ($i = 0; $i < $iterations; $i++) { $result = (string) $test; } }); ``` testToString(toString) Time: 2.008s Memory: 5.50Mb testToString(__toString) Time: 3.875s Memory: 5.50Mb testToString((string)) Time: 4.173s Memory: 5.50Mb The "why not to use another function instead" is almost twice slower. While the performance implication is always debatable, it's clear that allowing magic methods, __toString in particular, to accept default arguments has a real value for people who care to write dry and performant code. Previous Comments: ------------------------------------------------------------------------ [2010-03-12 08:31:43] olamedia at gmail dot com Why not to use another function instead of modifying system function? class Test { public function toString($bDebug = FALSE) { return $bDebug ? 'This is the debugging output' : 'This is the default output'; } public function __toString() { return $this->toString(); } } ------------------------------------------------------------------------ [2008-10-03 15:28:30] phpbugs at sevenlight dot com Description: ------------ I believe it should be possible to have optional arguments to the __toString() magic method to allow for explicitly changing the behaviour of this function. I understand why you cannot have any required arguments, but I do not see why it would not be possible to have optional arguments that would define the default behaviour. Reproduce code: --------------- class Test { public function __toString($bDebug = FALSE) { return $bDebug ? 'This is the debugging output' : 'This is the default output'; } } $o = new Test(); echo $o . "\n"; echo $o->__toString(TRUE) . "\n"; Expected result: ---------------- This is the default output This is the debugging output Actual result: -------------- Fatal error: Method Test::__tostring() cannot take arguments in /srv/www/test/toString.php on line 3 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=46225&edit=1

« previous php.bugs (#191381) next »