php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29446 PHP 5 allows class constants to be overriden
Submitted: 2004-07-29 17:56 UTC Modified: 2005-02-25 20:39 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: dimo dot vanchev at bianor dot com Assigned: tony2001 (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.3 OS: Windows 2000
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: dimo dot vanchev at bianor dot com
New email:
PHP Version: OS:

 

 [2004-07-29 17:56 UTC] dimo dot vanchev at bianor dot com
Description:
------------
If we declare a class constant twice or more PHP allows this without any notifications. This should be fixed, because it may bring to confusion. A constant should be constant, thus not allowing to be overriden neither in the same class, nor in child classes.

Currently non-class constants allow to be overriden too, at least they output a notice saying "Constant already defined". Maybe this should be brought up to warning.

Just my thoughts!

Reproduce code:
---------------
<?php
class A {
	const MY_CONSTANT = "A. value_1";
	const MY_CONSTANT = "A. value_2";
	
	public function __construct() {
		echo self::MY_CONSTANT, "\n";
	}
}

class B extends A {
	const MY_CONSTANT = "B. value_1";
	const MY_CONSTANT = "B. value_2";
	
	public function __construct() {
		echo self::MY_CONSTANT, "\n";
	}
}

new A();
new B();
?>

Expected result:
----------------
should display warning, error or something like that.

Actual result:
--------------
A. value_2
B. value_2


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-09-10 15:11 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2004-09-10 15:12 UTC] tony2001@php.net
I mean FIXED in CVS, try latest snapshots.
 [2004-09-24 09:51 UTC] dimo dot vanchev at bianor dot com
seems to work fine now, 
great many thanks for improving PHP's quality 
 
best regards
 [2005-02-25 14:59 UTC] dimo dot vanchev at bianor dot com
This bug doesn't seem to be fixed in 5.0.3 version, at least for the Windows version.
I haven't tryed to reproduce this bug under Linux for 5.0.3 version yet.
 [2005-02-25 20:39 UTC] tony2001@php.net
Yes, it was fixed only in HEAD (i.e. 5.1.0-dev), as this change can affect some existing scripts.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 06 09:01:32 2025 UTC