php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81582 Stringable not implicitly declared if __toString() came from a trait
Submitted: 2021-11-01 09:46 UTC Modified: -
From: kalle@php.net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
 [2021-11-01 09:46 UTC] kalle@php.net
Description:
------------
The `Stringable` interface available as of 8.0.0, which can be explicitly or implicitly available on a class that implements `__toString()` does not seem to acknowledge that if `__toString()` comes via a trait, then the implicit implementation of `Stringable` does not work

Test script:
---------------
<?php

trait A {
    public function __toString(): string {
        return 'ok';
    }
}

class B {
    use A;
}

class C {
    public function __toString(): string {
        return 'ok';
    }
}

$b = new B();

var_dump($b instanceof Stringable);

$c = new C();

var_dump($c instanceof Stringable);

Expected result:
----------------
PHP7:
bool(false)
bool(false)

PHP8:
bool(true)
bool(true)

Actual result:
--------------
PHP7:
bool(false)
bool(false)

PHP8:
bool(false)
bool(true)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-04 10:59 UTC] git@php.net
Automatic comment on behalf of nikic
Revision: https://github.com/php/php-src/commit/d3932682e7ad02a4a5aed617ae9060a8c3b6bc84
Log: Fixed bug #81582
 [2021-11-04 10:59 UTC] git@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 17:01:58 2024 UTC