Bug #81582 [Opn->Csd]: Stringable not implicitly declared if __toString() came from a trait

From: Date: Thu, 04 Nov 2021 10:59:11 +0000
Subject: Bug #81582 [Opn->Csd]: Stringable not implicitly declared if __toString() came from a trait
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237546@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81582&edit=1

 ID:                 81582
 Updated by:         git@php.net
 Reported by:        kalle@php.net
 Summary:            Stringable not implicitly declared if __toString()
                     came from a trait
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Irrelevant
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

Automatic comment on behalf of nikic
Revision: https://github.com/php/php-src/commit/d3932682e7ad02a4a5aed617ae9060a8c3b6bc84
Log: Fixed bug #81582


Previous Comments:
------------------------------------------------------------------------
[2021-11-01 09:46:10] 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)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=81582&edit=1


Thread (2 messages)

« previous php.bugs (#237546) next »