|  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
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
32 - 2 = ?
Subscribe to this entry?

 [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
 [2022-11-30 05:16 UTC] austinpatrick711 at gmail dot com
A really good post, very thankful and hopeful that you will write many more posts like this one.		

 [2022-12-22 06:15 UTC] bay23erruff at gmail dot com
That was a great. (
 [2022-12-26 11:17 UTC] Megan885Mershon at gmail dot com
Automation is a process where one software component communicates with and/or controls another software component using Microsoft's Component Object Model (COM). It is the basis for most cross-component communication used in languages such as Visual Basic or Visual Basic for Applications, and has become a normal part of most programs.

 [2024-04-23 18:53 UTC] lawleitl22 at gmail dot com
Nice post. I was checking constantly this blog and I am impressed! Extremely helpful information especially the last part I care for such info a lot.
Thank you and good luck.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 24 20:01:29 2024 UTC