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
Votes:1
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
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
 [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.		

(https://www.tellpopeyes.ltd/)php.net
 [2022-12-22 06:15 UTC] bay23erruff at gmail dot com
That was a great. (https://www.aceflare.net/)github.com
 [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.

(https://www.dunkinrunsonyou.biz/)github.com
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 08:01:28 2024 UTC