Bug #77488 [Fbk->Opn]: __toString in a trait

From: Date: Sat, 19 Jan 2019 09:48:04 +0000
Subject: Bug #77488 [Fbk->Opn]: __toString in a trait
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-219083@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77488&edit=1 ID: 77488 User updated by: alywena at gmail dot com Reported by: alywena at gmail dot com Summary: __toString in a trait -Status: Feedback +Status: Open Type: Bug Package: Scripting Engine problem Operating System: Windows 10 PHP Version: 7.3Git-2019-01-18 (Git) Block user comment: N Private report: N New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [2019-01-19 00:02:29] requinix@php.net And are you sure *that exact code* reproduces the problem? ------------------------------------------------------------------------ [2019-01-18 23:59:12] danack@php.net Are you seeing this on any released version, or only against master? ------------------------------------------------------------------------ [2019-01-18 23:52:19] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77488&edit=1

« previous php.bugs (#219083) next »