php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76725 Self-referencing constant fatal error in specific case
Submitted: 2018-08-09 12:57 UTC Modified: 2021-03-03 11:38 UTC
Votes:4
Avg. Score:4.2 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:2 (50.0%)
From: martin dot ninov at gmail dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 7.2.8 OS: Ubuntu16.04 and W10
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: martin dot ninov at gmail dot com
New email:
PHP Version: OS:

 

 [2018-08-09 12:57 UTC] martin dot ninov at gmail dot com
Description:
------------
The test script generates a Fatal error: Uncaught Error: Cannot declare self-referencing constant 'self::FOO'

All lines of test script are required, except the last line, which is just there to output something.

Removing any of the following eliminates the error:
namespace
second constant
constant assignment

I tested on a number on both win10 and linux(ubuntu 16.04), and a number of php 7.2 versions.

This: https://3v4l.org/3aIHb
says it works on php5.6 versions and fails in php7.0+

Test script:
---------------
<?php

namespace Waffles;

abstract class A
{
    const FOO = self::FOO;

    const OTHER = 0;

    private $waf = self::OTHER;
}

class B extends A
{
    const FOO = 'waffles';
}

$foo = new B();

echo $foo::FOO;


Expected result:
----------------
waffles

Actual result:
--------------
Fatal error: Uncaught Error: Cannot declare self-referencing constant 'self::FOO' 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-09 13:01 UTC] nikic@php.net
-Status: Open +Status: Feedback -Package: *General Issues +Package: Testing related
 [2018-08-09 13:01 UTC] nikic@php.net
What exactly is the bug here? That there is no error if the property declaration is removed?
 [2018-08-09 13:02 UTC] nikic@php.net
-Package: Testing related +Package: Scripting Engine problem
 [2018-08-09 13:05 UTC] martin dot ninov at gmail dot com
The bug is that somehow a bunch of unrelated statements cause the code to fail.

Also, I misspoke when I said 'constant assignment', I meant the 'property declaration' indeed.
 [2018-08-09 13:24 UTC] nikic@php.net
-Status: Feedback +Status: Open -Package: Scripting Engine problem +Package: *General Issues
 [2018-08-09 13:24 UTC] nikic@php.net
So generally, the code either failing or not failing is fine in this case. It can fail because that constant declaration is clearly illegal, it can not fail because the constant is not actually used.

But there's definitely something fishy going on here. While the impact of the property declaration doesn't surprise me much (probably triggers a full class initializer resolution), I don't see how the namespace declaration should impact this.
 [2018-08-11 14:48 UTC] a at b dot c dot de
Dunno how relevant this is, but in 7.2.8 under Windows 10, it fails when run as a script, but succeeds (and prints "waffles") when entered with -a's interactive shell.
 [2021-03-03 11:38 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2021-03-03 11:38 UTC] nikic@php.net
This is fixed in PHP 8.1 in that the "namespace" no longer makes a difference, and no error is thrown in either case. The reason was a bug in the compile-time evaluator, which failed to replace self::OTHER during compilation if a namespace was used.

However, as said above, whether or not an error gets thrown in this case is unspecified, and the behavior may change in the future.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 18:02:40 2024 UTC