php.net |  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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sh-ya at ya dot ru
New email:
PHP Version: OS:

 

 [2021-05-13 12:55 UTC] sh-ya at ya dot ru
Description:
------------
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) {
  ["i"]=>
  int(0)
  ["b"]=>
  bool(true)
}

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 ...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-17 10:43 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-05-17 10:43 UTC] cmb@php.net
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
possible).

[1] <https://www.php.net/manual/en/language.oop5.inheritance.php>
[2] <https://3v4l.org/RsDcu>
 [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] cmb@php.net
-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] cmb@php.net
Maybe there is actually a bug, but I don't think it will be fixed.
 [2021-05-17 13:54 UTC] nikic@php.net
-Type: Bug +Type: Documentation Problem
 [2021-05-17 13:54 UTC] nikic@php.net
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] nikic@php.net
-Summary: the variance and traits +Summary: Early binding limitations insufficiently documented
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Aug 01 13:01:24 2021 UTC