[PHP-BUG] Req #70761 [NEW]: Fatal error if missing parent class - create "stub" class instead
[email protected] ("andreas at dqxtech dot net")
| Newsgroups | php.standards |
|---|---|
| Message-ID | <[email protected]> |
From: andreas at dqxtech dot net
Operating system: Linux
PHP version: 7.0.0RC5
Package: PHP Language Specification
Bug Type: Feature/Change Request
Bug description:Fatal error if missing parent class - create "stub" class instead
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 bug report at https://bugs.php.net/bug.php?id=70761&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70761&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70761&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70761&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=70761&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=70761&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=70761&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=70761&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=70761&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=70761&r=support
Expected behavior: https://bugs.php.net/fix.php?id=70761&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=70761&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=70761&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=70761&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70761&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=70761&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=70761&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=70761&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70761&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=70761&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=70761&r=mysqlcfg