|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-01-18 23:52 UTC] alywena at gmail dot com
Description:
------------
If an object has a __toString() method define in a trait, the object could not be converted to string.
Test script:
---------------
trait ToString
{
abstract public function toString(): string;
public function __toString(): string
{
return $this->toString();
}
}
class Foo
{
use ToString;
public function toString()
{
return 'bar';
}
}
$foo = new Foo();
echo $foo;
Expected result:
----------------
'bar'
Actual result:
--------------
Object of class Foo could not be converted to string
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 03:00:02 2025 UTC |
Description: ------------ If an object has a __toString() method define in a trait, the object could not be converted to string. Test script: --------------- <?php declare(strict_types=1); namespace Traits { trait ToString { abstract public function toString(): string; public function __toString(): string { return $this->toString(); } } } namespace Test { use Traits\ToString; class MyStr { use ToString; public function toString(): string { return 'mystr'; } } class Foo { use ToString; public function toString(): string { return 'foo'; } } $foo = new Foo(); echo $foo; echo PHP_EOL; $s = new MyStr(); echo $s; } Expected result: ---------------- foo mystr Actual result: -------------- foo Recoverable fatal error: Object of class Test\MyStr could not be converted to string