Cannot use "static" as method modifier in trait alias

Description

The as adaptation syntax used with use (traits) can only change the visibility of an imported method, or give it a new alias. It cannot be used to add the static modifier to a method: a trait method’s static-ness is fixed by how it is declared in the trait itself, and cannot be altered by the class that imports it.

Example

<?php

trait Greet
{
    public function hello()
    {
        echo 'hi';
    }
}

class X
{
    use Greet {
        hello as static greetStatically;
    }
}

?>

Literal Examples

  • Cannot use “static” as method modifier in trait alias

Solutions

  • Remove the static modifier from the as adaptation, and only change the visibility or the alias name.

  • Declare the method as static directly in the trait, if that is the intended behavior.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.