Skip to content

Undocumented behaviour change in 8.5: self/parent now accepted in intersection types #5639

Description

@pronskiy

Affected page

https://www.php.net/manual/en/language.types.declarations.php

Issue description

In 8.1–8.4, using self or parent in an intersection type is a compile-time fatal error. In 8.5 it compiles (in a normal class), as a side effect of #17755. I couldn't find this documented in UPGRADING, and the RFC still says these types are not allowed.

Could this be added to the docs?

Note on scope

The new behaviour does not apply uniformly (as I understand) it still fatals in traits, in closures (bound or unbound), for anonymous classes, and for static. Any doc note should mention this so readers don't assume self&X works everywhere.

Refs

Steps to reproduce

<?php

interface FactoryConfig {}

interface Factory
{
    public function make(): self&FactoryConfig;
}
  • 8.1.0 – 8.4.x: Fatal error: Type self cannot be part of an intersection type
  • 8.5.0 – 8.5.7: compiles with no error

https://3v4l.org/MC5jDN

Suggested fix

  • Add an UPGRADING entry for the change.
  • Update the manual (Type declarations), which still states self/parent/static "result in an error" — this likely belongs in php/doc-en.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugDocumentation contains incorrect information

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions