php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #46225 Allow for optional arguments to __toString() magic method
Submitted: 2008-10-03 15:28 UTC Modified: 2021-08-27 13:44 UTC
Votes:24
Avg. Score:4.0 ± 0.8
Reproduced:22 of 22 (100.0%)
Same Version:9 (40.9%)
Same OS:3 (13.6%)
From: phpbugs at sevenlight dot com Assigned: cmb (profile)
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.3.0alpha2 OS: OSX 10.5.5
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phpbugs at sevenlight dot com
New email:
PHP Version: OS:

 

 [2008-10-03 15:28 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-12 08:31 UTC] 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();
    }
}
 [2015-03-14 10:15 UTC] ianbytchek at gmail dot com
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.
 [2016-12-31 00:32 UTC] cmb@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem
 [2021-08-27 13:44 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-08-27 13:44 UTC] cmb@php.net
This feature request doesn't make any sense to me.  Users are not
supposed to call magic methods explicitly (except maybe to
override them and call the parent's method).  Thus I'm closing as
WONTFIX.

If anybody still wants to see optional arguments for __toString(),
please pursue the RFC process[1].

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 06:01:30 2024 UTC