Edit report at https://bugs.php.net/bug.php?id=63359&edit=1
ID: 63359 Updated by: larue...@php.net Reported by: maciej dot sz at gmail dot com Summary: Defining a interface const with value of another constant triggers fatal error Status: Open -Type: Bug +Type: Feature/Change Request Package: Scripting Engine problem Operating System: Linux PHP Version: 5.4.8 Block user comment: N Private report: N New Comment: change to feature request. Previous Comments: ------------------------------------------------------------------------ [2012-11-14 14:52:29] dagguh at gmail dot com This is especially painful when you are trying to work your way around lack of class literal. <? namespace pl\dagguh\building; class Kitchen implements Room { const CLASS_NAME = "pl\\dagguh\\building\\Kitchen"; } interface Room { const CLASS_NAME = "pl\\dagguh\\building\\Room"; }?> -------- You often need the class name for mocking frameworks. ------------------------------------------------------------------------ [2012-10-25 10:45:43] maciej dot sz at gmail dot com Description: ------------ When a constant is defined in an interface with a value of another class or interface constant, a fatal error is triggered: Fatal error: Cannot inherit previously-inherited or override constant FOO from interface IMyInterface in Bar.php on line 3 To reproduce this following conditions must be met (took me 2 days to figure this out): - the constant value must be defined as another class/interface constant - classes and interfaces must be autoloaded on demand - two classes must implement the interface - one class must inherit the other one - the super class must be instantiated before the sub class Seems like this is related to bug #33732 and bug #38286 but is still not fixed. Test script: --------------- File IMyInterface.php: ---------------------- <?php interface IMyInterface { // Use any class or interface constant as value of FOO. // I'll just use some constant available out of the box, to keep it simple: const FOO = FilesystemIterator::CURRENT_AS_PATHNAME; } File Foo.php: ---------------------- <?php class Foo implements IMyInterface {} File Bar.php: ---------------------- <?php class Bar extends Foo implements IMyInterface {} File index.php: ---------------------- <?php date_default_timezone_set('Europe/Warsaw'); spl_autoload_register(function($cname){ if ( class_exists($cname, false) || interface_exists($cname, false) ) { return; } require_once "{$cname}.php"; }); // Foo is super-class for Bar. // uncomment below line to trigger the error: $F = new Foo(); // this should run without errors at any time, but if Foo is instantiated // prior to this line, then the error is triggered: $B = new Bar(); Expected result: ---------------- No errors, the code is correct. Actual result: -------------- Fatal error: Cannot inherit previously-inherited or override constant FOO from interface IMyInterface in Bar.php on line 3 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=63359&edit=1