Edit report at https://bugs.php.net/bug.php?id=80000&edit=1
ID: 80000
Updated by: cmb@php.net
Reported by: ASchmidt at Anamera dot net
Summary: Child classes not exist if defined before parent
class
Status: Open
Type: Bug
Package: Class/Object related
Operating System: Windows x64 IIS
PHP Version: 7.4.9
Block user comment: N
Private report: N
New Comment:
This is documented[1] behavior:
| Unless autoloading is used, then 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.
The fact that the engine is more permissible in certain cases, is
nothing code should rely upon; if you define multiple classes in
the same file, put them in the proper order. On the other hand,
we cannot change the current behavior in stable release branches,
for BC reasons.
We *may* error on potentially unsupported class order in PHP 8,
but I see no particular point in doing so.
[1] <https://www.php.net/manual/en/language.oop5.inheritance.php>
Previous Comments:
------------------------------------------------------------------------
[2020-08-20 07:04:43] ASchmidt at Anamera dot net
Thank you! I followed your lead and found out - it's being "masked" by OpCache!
IF OpCache is running (in my production 7.3.21), then the output is "true/true/true" - but
I had been turning off extensions in 7.4.9 to track down nyriad other errors - which then exposed
the reported behavior, further adding to the confusion.
I now understand that this was NOT due to a recent change. However, I now feel even stronger that
this is far from being handled "transparently", given all the various factors interacting
to create ambiguity.
If a child class only works "sometimes" -- when all the stars align correctly, then I feel
this should be rejected outright at the place where the child class is defined. If the only
consistent "working" pattern requires the child class to FOLLOW the parent class, then PHP
should enforce that sequence and not permit child classes before parent classes.
------------------------------------------------------------------------
[2020-08-20 05:52:44] requinix@php.net
Doesn't look like anything's changed recently. https://3v4l.org/bIF1m
MySubclass1 can't be defined early because it depends on MyAbstract which isn't available
yet. MySubclass2 doesn't have that problem.
------------------------------------------------------------------------
[2020-08-20 05:47:25] ASchmidt at Anamera dot net
Description:
------------
Produced with a fresh PHP 7.4.9 instance, empty PHP.ini, and a fresh web site.
Child classes that are defined BEFORE the parent class can be referenced by code prior to the class
definitions if running PHP 7.3.21 - but it will FAIL as non-existing in 7.4.9.
I've been struggling with various warnings/errors in WordPress and other code that imply
invalid class/method references, and was finally able to extract the attached reproducable sample.
Test script:
---------------
<?php
declare(strict_types=1);
var_dump( class_exists( 'MySubclass1' ), class_exists( 'MySubclass2' ),
class_exists( 'MyAbstract' ) ); // PHP 7.4.9 = FALSE, true, true (PHP 7.3.21 = TRUE,
true, true)
class MySubclass1 extends MyAbstract{}
abstract class MyAbstract{}
class MySubclass2 extends MyAbstract{}
var_dump( class_exists( 'MySubclass1' ), class_exists( 'MySubclass2' ),
class_exists( 'MyAbstract' ) ); // true, true, true
// Produced with empty php.ini !
Expected result:
----------------
If a child class is defined before or after the parent class should yield consistent results,
whether the child class is referenced earlier or later in the code - as it was in 7.3.
IF it is NOT permitted to define "MySubclass1" before the parent class, then this should
lead to an error at the point the definition is encountered. ACCEPTING the definition as valid but
then producing chance errors depending solely on where the child class is referenced seems arbitrary
when working in large projects.
Actual result:
--------------
Depending on whether the child class was defined before or after the parent, and whether that child
class happens to be referenced before or after its definition, will yield different outcomes (exist
/ not exist)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80000&edit=1