php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70514 isset(aClass::ARRAY[$a]) is mistaken for an expression
Submitted: 2015-09-16 18:39 UTC Modified: 2015-10-11 04:22 UTC
From: dams@php.net Assigned:
Status: No Feedback Package: Class/Object related
PHP Version: 7.0.0RC2 OS: OSX, Debian
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dams@php.net
New email:
PHP Version: OS:

 

 [2015-09-16 18:39 UTC] dams@php.net
Description:
------------
Since PHP 5.6, constants may be arrays (class or global constants). However, isset() recognize them as 'expressions' and produces a fatal error. 

       if (isset(aClass::X[$a]))
       if (isset(X[$a]))

On the other hand, PHP accepts :
       if (isset(aClass::$X[$a])) 


The behavior is in PHP 5.6 till PHP 7.0RC2 (and I tried PHP 7.1.0-dev this afternoon).


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

class W {
    const X = [1 => 2, 3 => 4];
    
    public function f() {
       $a = 5; 
       if (isset(static::X[$a])) {
            print "$a exists\n";
       } else {
            print "$a doesn't exists\n";
       }
    }
}

$x = new X;
$x->f();


Expected result:
----------------
       if (isset(aClass::X[$a]))
       if (isset(X[$a]))

works just like 

       if (isset(aClass::$X[$a])) 


Actual result:
--------------
The error : 
PHP Fatal error:  Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in /Users/famille/Desktop/analyze/test.php on line 9

Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in /Users/famille/Desktop/analyze/test.php on line 9


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-09-17 01:18 UTC] feilengcui at gmail dot com
hello,I just test your code for 7.0RC1 and 7.0RC2, and I find that 
    if(isset(aClass::X[$a]))
    if(isset(X[$a]))
are both OK and output "5 doesn't exists"

but for 
    if(isset(aClass::$X[$a]))
it reports an error
    "Fatal error: Uncaught EngineException: Access to undeclared static property: W::$X"
 [2015-09-17 09:15 UTC] dams@php.net
OK, I made it to work on Debian with 7.1.0-dev. 

Recompiled on osx too, but still doesn't work.
 [2015-09-18 04:08 UTC] laruence@php.net
did I misunderstand something here? https://3v4l.org/upQIg

it seems works fine with php7
 [2015-09-18 17:47 UTC] iletisim at ilkerozcan dot com dot tr
Works on ubuntu but does not work on OsX. It may be due to the bison version.
 [2015-09-19 01:24 UTC] bwoebi@php.net
-Status: Open +Status: Feedback
 [2015-09-19 01:24 UTC] bwoebi@php.net
Works for me on OS X with PHP 7...

Just on PHP 5.6, isset() did not support constant arrays at all. I just had forgotten to take care of that special case back then.

isset(aClass::X) still does not work with PHP 7, but aClass::X[$a] (or static::X[$a]) definitely should work there.
 [2015-09-22 15:16 UTC] dams@php.net
I tried with bison 2.7 and 3.0.4, with the same result. 

I use this compilation sequence : 
make clean

./buildconf --force
env YACC=`brew --prefix bison27`/bin/bison 
./configure \
    --prefix="/usr/local/opt/phpng" \
    --with-config-file-path="/usr/local/etc/phpng" \
    --with-config-file-scan-dir=/usr/local/etc/phpng/conf.d
    --enable-mbstring \
    --enable-tokenizer \
    --disable-all
make -j`sysctl -n hw.logicalcpu_max`

on #master from php-src. 

It is working fine on Debian.
 [2015-09-28 09:10 UTC] dams@php.net
I confirm the situation from bwoebi : 

aClass::X[$a] (or static::X[$a]) work fine.

isset(aClass::X) still does not work. 

The revision is 250938e2d35fc54161a18167b7901c5e3b574371. 
I'll keep testing until the 2nd instruction is ok. The rest is good to for, AFAIK
 [2015-10-11 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2016-05-18 15:31 UTC] phansys at gmail dot com
This issue is present in 5.6.21 version too.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Mar 04 08:01:28 2024 UTC