phptools-docs icon indicating copy to clipboard operation
phptools-docs copied to clipboard

Checking `Override` attribute error `PHP2437` with overlay traits

Open zengbo opened this issue 10 months ago • 2 comments

Here is the code:

trait A
{
   abstract protected static function T();
}

trait B
{
  use A
  #[Override]
  protected static function T()
  {
      echo 'T';
  }
}

class TT
{
  use B;
}

The code works, however PHPTools displays an error message: 'B::T has #[Override] attribute, but no matching parent method exists (PHP2437)' in class TT.

zengbo avatar Jan 16 '25 06:01 zengbo

Thank you for the test case.

We'll try to fix that.


Just for a matter of interest - isn't it technically correct? class TT does not have a parent

jakubmisek avatar Jan 17 '25 18:01 jakubmisek

Thank you for the test case.

We'll try to fix that.

Just for a matter of interest - isn't it technically correct? class TT does not have a parent

I just simplified the code. The parent class of class TT shows the same error.:

abstract class T
{

}

trait A
{
   abstract protected static function T();
}

trait B
{
  use A
  #[Override]
  protected static function T()
  {
      echo 'T';
  }
}

class TT extends T
{
  use B;
}

zengbo avatar Jan 20 '25 02:01 zengbo

Another issue with __construct:

abstract class T
{
    public function __construct() {}
}

class TT extends T
{
    #[Override]
    public function __construct() {}
}

You have the same error PHP2437 for this use case.

Renji-FR avatar Nov 25 '25 18:11 Renji-FR

@jakubmisek I added another related use case with __construct.

Renji-FR avatar Nov 27 '25 09:11 Renji-FR

@Renji-FR __construct() method does not satisfy #[/Override] as specified by wiki.php.net/rfc/marking_overriden_methods , section Semantics

__construct() of a parent class do not satisfy #[\Override], because it's not part of the API of an already-constructed object.

jakubmisek avatar Nov 27 '25 11:11 jakubmisek

@zengbo, according to wiki.php.net/rfc/marking_overriden_methods#semantics:

the #[\Override] attribute on a trait method requires the existence of a matching method in a parent class or implemented interface.

So the PHP2437 error should be correct here.

jakubmisek avatar Nov 27 '25 11:11 jakubmisek