Edit report at http://bugs.php.net/bug.php?id=46225&edit=1

 ID:               46225
 Comment by:       olamedia 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

 New Comment:

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

    }

}


Previous Comments:
------------------------------------------------------------------------
[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 http://bugs.php.net/bug.php?id=46225&edit=1

Reply via email to