Req #70761 [Opn->Sus]: Fatal error if missing parent class - create "stub" class instead
| From: | nikic@php.net | Date: | Thu, 16 Mar 2017 18:22:17 +0000 |
| Subject: | Req #70761 [Opn->Sus]: Fatal error if missing parent class - create "stub" class instead | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-207878@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70761&edit=1
ID: 70761
Updated by: nikic@php.net
Reported by: andreas at dqxtech dot net
Summary: Fatal error if missing parent class - create "stub"
class instead
-Status: Open
+Status: Suspended
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: Linux
PHP Version: 7.0.0RC5
Block user comment: N
Private report: N
New Comment:
Marking as suspended as such a change would require an RFC (https://wiki.php.net/rfc/howto).
Personally, I very much doubt this is going to happen. This is a lot of complexity for an edge-case,
and will additionally convert currently obvious failures (typo in extends clause) into a lot of head
scratching about classes silently not being defined for unknown reasons.
Previous Comments:
------------------------------------------------------------------------
[2015-12-06 07:53:17] andreas at dqxtech dot net
Related To: Bug #71037
------------------------------------------------------------------------
[2015-10-21 17:19:16] andreas at dqxtech dot net
Description:
------------
(similar to https://bugs.php.net/bug.php?id=53036&edit=2,
but more relevant issue summary - * see below)
Currently if a script wants to declare a class, and inherit from a missing parent class, a fatal
error will occur.
This causes problems with class_exists() + autoload:
- If class C cannot be autoloaded, then class_exists('C') will simply return false.
- If class C can be autoloaded, but the parent class P can not, then class_exists('C')
will include the file that defines C, which triggers a fatal error because P does not exist and
cannot be autoloaded.
Typical scenario:
- Library LibC contains a class C with parent class P, where P is defined in another library LibP.
- Library LibC does NOT explicitly depend on LibP. Instead, the class C should only be used if LibP
is installed in the project. We could say, LibP is an "optional dependency" of LibC.
- A discovery algorithm scans LibC for useful classes, and tries
"class_exists('C')", or explicitly includes the file defining C -> fatal
error, because P is missing.
Instead, such discovery algorithms nowadays need to use PHP parsing and stuff like Doctrine's
StaticReflectionParser. https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/Reflection/StaticReflectionParser.php
Solution:
- Instead of a fatal, simply behave as if the class C is not defined. So,
class_exists('C') should return FALSE.
- Internally remember the "stub" definition of C.
- If class_exists('C') is called again, and autoloading for 'C' would be
triggered again, instead trigger the autoload for 'P' and attempt to complete the stub
definition of C.
- Provide a new language feature to test for "stub" class definitions.
---------
* Why a new issue, and not comment on https://bugs.php.net/bug.php?id=53036&edit=2
?
The other issue paints this as a problem with class_exists() + autoload. But solution is to change
the behavior of include(), independent of class_exists() + autoload. The description and code
example in this issue focuses directly on the include(). I expect this to allow for a more focused
discussion.
Test script:
---------------
// Assuming class P does not exist and cannot be autoloaded either.
class C extends P {}
print class_exists('C') ? 'yes' : 'no';
Expected result:
----------------
'no'
(because C has a non-existing base class)
Actual result:
--------------
PHP Fatal error: Class 'P' not found [..]
(triggered by the class definition, NOT by the class_exists())
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70761&edit=1