php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74044 Parse error when using $this::CONSTANT in Heredoc
Submitted: 2017-02-05 09:38 UTC Modified: 2019-08-22 06:51 UTC
From: olivier dot laviale at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: Irrelevant OS: MacOS
Private report: No CVE-ID: None
 [2017-02-05 09:38 UTC] olivier dot laviale at gmail dot com
Description:
------------
Using {$this::CONSTANT} in Heredoc causes a parse error.


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

class A
{
    const A = 'a';
    static private $c;
    
    public function render()
    {
        return <<<EOT
'{$this::$c}'
'{$this::A}'
EOT;
    }
}

Expected result:
----------------
{$this::A} should yield the constant value in Heredoc.

Actual result:
--------------
7.0.0 - 7.1.1: Parse error: syntax error, unexpected '}', expecting '['
5.6.0 - 5.6.30: Parse error: syntax error, unexpected '}', expecting '('


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-08-22 06:51 UTC] cmb@php.net
-Summary: Parse error when using $this::CONSTRANT in Heredoc +Summary: Parse error when using $this::CONSTANT in Heredoc -Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2019-08-22 06:51 UTC] cmb@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

From the docs[1]:

| When a string is specified in double quotes or with heredoc,
| variables are parsed within it.

A constant isn't a variable.

[1] <https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 17:01:30 2024 UTC