php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73348 A class can override a constant inherited from an indirect parent interface.
Submitted: 2016-10-19 13:08 UTC Modified: 2021-11-18 22:28 UTC
Votes:6
Avg. Score:3.7 ± 0.9
Reproduced:5 of 5 (100.0%)
Same Version:1 (20.0%)
Same OS:2 (40.0%)
From: mkh117 at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Class/Object related
PHP Version: 7.0.12 OS: Ubuntu 16.04.1
Private report: No CVE-ID: None
 [2016-10-19 13:08 UTC] mkh117 at gmail dot com
Description:
------------
When a class inherits a constant from an interface that is not a direct parent of the class, it can override its value without any errors thrown.

Test script:
---------------
<?php
interface a {
  const con = 'Original value';	
}

abstract class b implements a {

}

class d extends b {
  const con='Overridden value';
}

echo d::con;

Expected result:
----------------
Fatal error:  Cannot inherit previously-inherited constant con from interface a ... on line 11

Actual result:
--------------
The script works and the interface constant is overridden.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-19 15:17 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2016-10-19 15:17 UTC] cmb@php.net
Confirmed: <https://3v4l.org/u4Y1q>.
 [2016-10-21 15:42 UTC] leigh@php.net
Related:

interface A {}
abstract class B implements A {}
class C extends B implements A {}

I would expect this to generate: "Class %s cannot implement previously implemented interface %s"

Both this and the constant override issue are handled by zend_do_implement_interface which is not called for for grandchildren implementors
 [2016-10-21 15:57 UTC] leigh@php.net
Actually that might not be a bug, possibly designed to prevent:

class B implements A, A {}
 [2021-09-23 15:00 UTC] dktapps at pmmp dot io
Is there any discussion on what to do about this problem? I just learned that a project of mine (with an API stable for 3 years) unintentionally exploited this bug, so I'd love to get an idea of whether I need to break BC to avoid my house burning down on a patch release.
 [2021-11-18 22:28 UTC] cmb@php.net
-Status: Verified +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-11-18 22:28 UTC] cmb@php.net
Actually, this is not a bug, but rather the fact that constants
defined in interfaces could not be overridden was.  This is all
resolved as of PHP 8.1.0[1] due to the introduction of final
constants[2].

> […], so I'd love to get an idea of whether I need to break BC to
> avoid my house burning down on a patch release.

No worries. :)

[1] <https://3v4l.org/c5dmY>
[2] <https://wiki.php.net/rfc/final_class_const>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 08:01:28 2024 UTC