php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63359 Defining a interface const with value of another constant triggers fatal error
Submitted: 2012-10-25 10:45 UTC Modified: 2018-01-14 13:17 UTC
Votes:21
Avg. Score:4.8 ± 0.5
Reproduced:21 of 21 (100.0%)
Same Version:8 (38.1%)
Same OS:13 (61.9%)
From: maciej dot sz at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4.8 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: maciej dot sz at gmail dot com
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-11-14 14:52 UTC] 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-11-14 15:21 UTC] laruence@php.net
change to feature request.
 [2012-11-14 15:21 UTC] laruence@php.net
-Type: Bug +Type: Feature/Change Request
 [2012-11-14 16:37 UTC] maciej dot sz at gmail dot com
How come a feature request? This is an obvious bug.
 [2012-11-19 11:59 UTC] aharvey@php.net
Automatic comment from SVN on behalf of aharvey
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=328423
Log: Warn about configure options that were deprecated in 5.3 and removed in 5.4.

Fixes doc bug #63359 (configure: WARNING: unrecognized options:
--enable-zend-multibyte).
 [2013-01-14 11:07 UTC] edmond at inbox dot ru
Some problem: OS Windows 7 64-bits
 [2013-05-22 08:36 UTC] jan dot kahoun at heureka dot cz
Same problem with PHP 5.4.13 on CentOS release 6.4 (Final)
 [2013-10-01 10:52 UTC] dagguh at gmail dot com
This is a bug. No questions about it.

This bug is caused by: https://bugs.php.net/bug.php?id=49472
The fix is all wrong. PHP developers do not seem to understand basic concepts in OOP. Const is a static member. Being static means NO 
POLYMORPHISM. They should NEVER BE INHERITED. Each class/interface should be able to define their own static members DISREGARDING any 
possible class hierarchy.
 [2015-10-14 12:20 UTC] maciej dot sz at gmail dot com
@dagguh: static members in PHP are inherited and it is by design, so you can get used to it. It's just a PHP's nature. The problem starts when this behaviour causes bugs like the one described above.

Anyway the issue seems to be fixed in 7.0 (checked with 7.0.0-RC5).
 [2016-08-02 17:33 UTC] argon dot test at yandex dot ru
Same problem in LATEST php5.6 version! This is terrible bug.
$ php -v
PHP 5.6.24-1+deb.sury.org~trusty+1 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
 [2018-01-14 13:17 UTC] nikic@php.net
-Status: Open +Status: Closed -Type: Feature/Change Request +Type: Bug -Assigned To: +Assigned To: nikic
 [2018-01-14 13:17 UTC] nikic@php.net
As noted, this has been fixed in PHP 7 (https://3v4l.org/VpWO9), so closing this issue.
 [2018-02-07 08:10 UTC] jonik dot lowkick at gmail dot com
Seems it still not fixed.
PHP versions: 7.0.27, 7.1.13, 7.2.2.

https://3v4l.org/7ARJX
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Mar 12 07:01:32 2025 UTC