|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-10-25 10:45 UTC] 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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 22 21:00:01 2025 UTC |
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.