php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77488 __toString in a trait
Submitted: 2019-01-18 23:52 UTC Modified: 2019-01-19 10:18 UTC
From: alywena at gmail dot com Assigned:
Status: Duplicate Package: Scripting Engine problem
PHP Version: 7.3.0 OS: Windows 10
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: alywena at gmail dot com
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-01-18 23:59 UTC] danack@php.net
Are you seeing this on any released version, or only against master?
 [2019-01-19 00:02 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2019-01-19 00:02 UTC] requinix@php.net
And are you sure *that exact code* reproduces the problem?
 [2019-01-19 09:48 UTC] alywena at gmail dot com
-Status: Feedback +Status: Open
 [2019-01-19 09:48 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:
---------------
<?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 10:07 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2019-01-19 10:07 UTC] requinix@php.net
Exactly what version of PHP are you using?
 [2019-01-19 10:16 UTC] alywena at gmail dot com
The 7.3.0 include in XAMPP 7.3.0.

In 7.2.4 the problem not occure.
 [2019-01-19 10:18 UTC] requinix@php.net
-Status: Feedback +Status: Duplicate -PHP Version: 7.3Git-2019-01-18 (Git) +PHP Version: 7.3.0
 [2019-01-19 10:18 UTC] requinix@php.net
Duplicate of bug #77291 which was fixed in 7.3.1.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 04 08:01:29 2024 UTC