'php - can't use as alias for functions with same name

I am getting the following error with "use ... as ..." syntax

Fatal error: An alias was defined for method getRoles(), which exists in both MembersBundle\Adapter\User\UserTrait and FrontendPermissionToolkitBundle\CoreExtensions\Traits\PermissionResourcesAsRolesTrait. Use MembersBundle\Adapter\User\UserTrait::getRoles or FrontendPermissionToolkitBundle\CoreExtensions\Traits\PermissionResourcesAsRolesTrait::getRoles to resolve the ambiguity in **/Model/DataObject/CombinedUserAndCustomer.php on line 17

Here is the file:

<?php

declare(strict_types=1);

namespace CosmicCoreBundle\Model\DataObject;

use CoreShop\Component\Core\Model\Customer;
use FrontendPermissionToolkitBundle\CoreExtensions\Traits\PermissionResourcesAsRolesTrait;
use MembersBundle\Adapter\User\UserInterface;
use MembersBundle\Adapter\User\UserTrait;
use CustomerManagementFrameworkBundle\Model\CustomerInterface;
use CustomerManagementFrameworkBundle\Model\Traits\CustomerTrait;

/**
 * Combines the members user with the CoreShop customer
 */
abstract class CombinedUserAndCustomer extends Customer implements UserInterface, CustomerInterface
{
    use UserTrait {
        getRoles as getMembersRoles;
    }

    use PermissionResourcesAsRolesTrait {
        getRoles as getPermissionResourceRoles;
    }

    use CustomerTrait;

    /**
     * Retrieve all allowed permission resources of current object prefixed with ROLE_
     *
     * @return string[]
     */
    public function getRoles(): array
    {
        // Combine CoreShop roles with members and permission resource roles
        return array_merge(
            parent::getRoles(),
            $this->getMembersRoles(),
            $this->getPermissionResourceRoles()
        );
    }
}

It's in a pimcore installation (symfony 5 framework) and i use php8.1 I would be glad if someone can explain what is the problem. thanks ^^



Solution 1:[1]

In addition to @MEriksson comment you have to rewrite your code like that that you avoid functionname conflict. like that:

abstract example code

<?php

trait UserTrait {
    public function getRoles() {
        echo 'roleFromUserTrait';
    }

    public function make1() {
        echo 'A';
    }
}

trait PermissionResourcesAsRolesTrait {
    public function getRoles() {
        echo 'roleFromPermissionResourcesAsRolesTrait';
    }
    public function make2() {
        echo 'B';
    }
}

class Hello {
    use UserTrait, PermissionResourcesAsRolesTrait {
        UserTrait::getRoles insteadof PermissionResourcesAsRolesTrait;
        UserTrait::getRoles as getR1;
        PermissionResourcesAsRolesTrait::getRoles insteadof UserTrait;
        PermissionResourcesAsRolesTrait::getRoles as getR2;        
    }
}

$h = new Hello();
print_r($h->getR1());
print_r($h->getR2());

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maik Lowrey