Skip to content

Promoted trait interface #11435

@mvorisek

Description

@mvorisek

Description

There are valid usecases where traits, multi-inheritance, define public interface.

Currently checking if a class implements a trait is complex as it must traverse all uses in all parent classes using a custom code. The best solution to check for some method is simply an explicitly defined interface, but currently it requires an interface to be manually added to the class where trait is added. This requires more code and the explicit interface can be even omitted by a mistake

This is a feature request to allow to specify an interface in trait directly. When such trait is used, it will promote the interface to the class where used. And same as with abstract classes, it will be required to be implemented no later than in the 1st non-abstract class.

<?php

interface I {
    public function f(): void;
}

// (already supported) abstract class does not have to implement interface
abstract class X implements I {}

// trait should be able to define interface, but it doesn't need to implemented
// until used in non-abstract class
trait T implements I {}

abstract class X2 {
    use T;
}

class X3 extends X2 {
    public function f(): void {
    }
}

var_dump((new X3()) instanceof T); // must print true

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions