Bug #81582 [NEW]: Stringable not implicitly declared if __toString() came from a trait

From: Date: Mon, 01 Nov 2021 09:46:10 +0000
Subject: Bug #81582 [NEW]: Stringable not implicitly declared if __toString() came from a trait
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237482@lists.php.net to get a copy of this message
From:             kalle
Operating system: Irrelevant
PHP version:      Irrelevant
Package:          Scripting Engine problem
Bug Type:         Bug
Bug description:Stringable not implicitly declared if __toString() came from a trait

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 bug report at https://bugs.php.net/bug.php?id=81582&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=81582&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=81582&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=81582&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=81582&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=81582&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=81582&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=81582&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=81582&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=81582&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=81582&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81582&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=81582&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=81582&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=81582&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=81582&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=81582&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=81582&r=mysqlcfg


Thread (2 messages)

« previous php.bugs (#237482) next »