php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81497 Multiple traits can not be aliased since PHP 8.0
Submitted: 2021-10-03 13:00 UTC Modified: 2021-10-03 14:23 UTC
From: michael dot vorisek at email dot cz Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 8.0.11 OS: any
Private report: No CVE-ID: None
 [2021-10-03 13:00 UTC] michael dot vorisek at email dot cz
Description:
------------
demo https://3v4l.org/eT0Aj (same as test case below)

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

trait ATrait {
    public function __isset(string $name): bool
    {
        return false;
    }
}

trait BTrait {
    public function __isset(string $name): bool
    {
        return false;
    }
}

class Model
{
    use ATrait {
        __isset as private __a_isset;
    }
    use BTrait {
        __isset as private __b_isset;
    }
    
    public function __isset(string $name): bool
    {
        return false;
    }
}

Expected result:
----------------
no fatal error

Actual result:
--------------
Fatal error: An alias was defined for method __isset(), which exists in both ATrait and BTrait. Use ATrait::__isset or BTrait::__isset to resolve the ambiguity in /in/eT0Aj on line 17

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-10-03 13:17 UTC] michael dot vorisek at email dot cz
even one intermediate trait with alias is not enough https://3v4l.org/8gZrO

the only supported solution for PHP 8.0 is to alias all traits inside a separate traits https://3v4l.org/hmnWC

all 3 test cases should be supported
 [2021-10-03 14:04 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2021-10-03 14:04 UTC] nikic@php.net
The error message literally tells you what you need to do: https://3v4l.org/lEgvo
 [2021-10-03 14:10 UTC] michael dot vorisek at email dot cz
another failing test case https://3v4l.org/cUp5N
 [2021-10-03 14:15 UTC] michael dot vorisek at email dot cz
> The error message literally tells you what you need to do: https://3v4l.org/lEgvo

Thanks a lot!, why a trait name needs to be specified for the alias? I mean what is the logic behind, which ambiguity it solves?
 [2021-10-03 14:23 UTC] nikic@php.net
Trait use blocks are meaningless.

    use ATrait {
        __isset as private __a_isset;
    }
    use BTrait {
        __isset as private __b_isset;
    }

is equivalent to

    use ATrait, BTrait {
        __isset as private __a_isset;
        __isset as private __b_isset;
    }

which is more obviously ambiguous. While your code was accepted prior to PHP 8, it did not actually do what you intended (it would create two aliases to ATrait::__isset instead.)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 07:01:30 2024 UTC