Bug #77488 [Com]: __toString in a trait

From: Date: Sat, 19 Jan 2019 10:16:20 +0000
Subject: Bug #77488 [Com]: __toString in a trait
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-219085@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 Comment by: alywena at gmail dot com Reported by: alywena at gmail dot com Summary: __toString in a trait Status: Feedback 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: The 7.3.0 include in XAMPP 7.3.0. In 7.2.4 the problem not occure. Previous Comments: ------------------------------------------------------------------------ [2019-01-19 10:07:05] requinix@php.net Exactly what version of PHP are you using? ------------------------------------------------------------------------ [2019-01-19 09:48:03] 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: --------------- <?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 ------------------------------------------------------------------------ [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 (#219085) next »