php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69676 Resolution of self::FOO in class constants not correct
Submitted: 2015-05-20 21:55 UTC Modified: 2017-01-08 11:08 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:0 (0.0%)
From: nikic@php.net Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: master-Git-2015-05-20 (Git) OS:
Private report: No CVE-ID: None
 [2015-05-20 21:55 UTC] nikic@php.net
Description:
------------
* Resolution of self:: at compile-time for constants currently doesn't seem to happen in PHP 7
* The run-time resolution uses the wrong self:: scope in PHP 7
* The scope is also wrong in PHP 5, but in a different way (depending on whether A:: or B:: is used).

Test script:
---------------
<?php
class A {
	const myConst = "const in A";
	const myDynConst = self::myConst;
}

class B extends A {
	const myConst = "const in B";
}

var_dump(B::myDynConst);
var_dump(A::myDynConst);

Expected result:
----------------
string(10) "const in A"
string(10) "const in A"

Actual result:
--------------
PHP 7:
string(10) "const in B"
string(10) "const in B"

PHP 5:
string(10) "const in A"
string(10) "const in B"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-20 21:55 UTC] nikic@php.net
-Assigned To: +Assigned To: nikic
 [2017-01-08 11:08 UTC] nikic@php.net
The particular test script works in PHP 7, but the following still fails:

<?php
class A {
	const myDynConst = self::myConst;
	const myConst = "const in A";
}

class B extends A {
	const myConst = "const in B";
}

var_dump(B::myDynConst);
var_dump(A::myDynConst);
 [2017-03-15 15:18 UTC] fredemmott@php.net
As noted on php-internals: PHP7 behavior actually depends on ordering:

<?php

class Foo {
    const A = 'Foo::A';
    const B = self::A . ' and ' . self::C;
    const C = 'Foo::C';
    
}

class Bar extends Foo {
    const A = 'Bar::A';
    const C = 'Bar::C';
}

var_dump(Bar::B);

Output: string(17) "Foo::A and Bar::C"
 [2017-03-15 18:50 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=2bba4a0d7f6d5e5712d60bc1cf2119622d837e55
Log: Fix bug #69676
 [2017-03-15 18:51 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC