Skip to content

Incorrect type handling of 'self' in 'traits' #1201

@Danil42Russia

Description

@Danil42Russia
<?php

trait TrainA {
  private ?string $name = null;

  public function name(string $name): self {
    $this->name = $name;

    return $this;
  }
}

class ClassA {
  use TrainA;

  private ?string $description = null;

  public function description(string $description): self {
    $this->description = $description;

    return $this;
  }

  public function __toString(): string {
    return "name: " . $this->name . PHP_EOL .
      "description: " . $this->description . PHP_EOL;
  }
}

function main() {
  $a = (new ClassA())
    ->name("name")
    ->description("description");

  echo $a;
}

main();

Actual Behavior:

<critical> ERROR   undefinedMethod: Call to undefined method {\TrainA}->description() at index.php:33
    ->description("description");
      ^^^^^^^^^^^

Expected Behavior:

No error

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions