|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #81038 Early binding limitations insufficiently documented
Submitted: 2021-05-13 12:55 UTC Modified: 2021-05-27 08:29 UTC
From: sh-ya at ya dot ru Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 7.4.19 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: sh-ya at ya dot ru
New email:
PHP Version: OS:


 [2021-05-13 12:55 UTC] sh-ya at ya dot ru
using the variance requires pre-definition of classes that use traits

Test script:
interface Animal {
	public function eat(AnimalFood $food);

class Dog implements Animal {
	public function eat(Food $food) {}

trait SomeTrait {
	public $b = true;

class Food {
	use SomeTrait;
	public $i = 0;

class AnimalFood extends Food {}

var_dump(new AnimalFood);

Expected result:
object(AnimalFood)#1 (2) {

Actual result:
Fatal error: Could not check compatibility between Dog::eat(Food $food) and Animal::eat(AnimalFood $food), because class AnimalFood is not available in ...


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-17 10:43 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-05-17 10:43 UTC]
The problem is the order of the definitions.  Animal cannot be
resolved until AnimalFood is defined, and that trips up the
script.  This is basically documented[1]:

| Unless autoloading is used, the classes must be defined before
| they are used. If a class extends another, then the parent class
| must be declared before the child class structure. This rule
| applies to classes that inherit other classes and interfaces.

So yes, the best solution is to use autoloading; alternatively you
can fix the order of definitions[2] (that may not always be

[1] <>
[2] <>
 [2021-05-17 12:58 UTC] sh-ya at ya dot ru
comment out "use SomeTrait" and that order will work
 [2021-05-17 13:13 UTC]
-Status: Not a bug +Status: Open -Type: Documentation Problem +Type: Bug -Package: Documentation problem +Package: Scripting Engine problem -Assigned To: cmb +Assigned To:
 [2021-05-17 13:13 UTC]
Maybe there is actually a bug, but I don't think it will be fixed.
 [2021-05-17 13:54 UTC]
-Type: Bug +Type: Documentation Problem
 [2021-05-17 13:54 UTC]
Traits (and for that matter interfaces) prevent early binding. Early binding if a legacy feature that we have no plans to extend. You should either declare classes in the correct order or use autoloading.

As such, I am moving this back to docs.
 [2021-05-27 08:29 UTC]
-Summary: the variance and traits +Summary: Early binding limitations insufficiently documented
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Jul 04 23:05:45 2022 UTC