New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[php5.6] Constant arrays not supported #4277

Closed
hikari-no-yume opened this Issue Nov 18, 2014 · 30 comments

Comments

Projects
None yet
@hikari-no-yume

hikari-no-yume commented Nov 18, 2014

@fredemmott

This comment has been minimized.

Contributor

fredemmott commented Nov 18, 2014

@fredemmott

This comment has been minimized.

Contributor

fredemmott commented Nov 18, 2014

The fix for this should presumably also support Hack collection literals in Hack files (or with hhvm.force_hh enabled).

@paulbiss

This comment has been minimized.

Contributor

paulbiss commented Nov 18, 2014

Fix is up internally D1688260

@jrobeson

This comment has been minimized.

Contributor

jrobeson commented Dec 8, 2014

will it automatically close github bugs when internal fixes are landed?

@Majkl578

This comment has been minimized.

Contributor

Majkl578 commented Dec 8, 2014

Yes, usually.

@paulbiss

This comment has been minimized.

Contributor

paulbiss commented Dec 9, 2014

Haven't had a chance to fix remaining test failures, but code is now here for the curious: https://reviews.facebook.net/D29163

@hikari-no-yume

This comment has been minimized.

hikari-no-yume commented Dec 21, 2014

define() now supports them in php-src master (it'll be in PHP 7.0), so whoever adds support for this in HHVM should make sure it works with define() too: php/php-src@0833fd4

@Kozzi11

This comment has been minimized.

Kozzi11 commented Aug 4, 2015

@paulbiss what's the current status?

@hikari-no-yume

This comment has been minimized.

hikari-no-yume commented Dec 5, 2015

Did this get in?

@hopeseekr

This comment has been minimized.

hopeseekr commented Apr 26, 2016

I am pretty sure that this is still not supported :-((

@Webysther

This comment has been minimized.

Webysther commented May 18, 2016

There is an alternative to get around the problem that can be reversed when it is fixed?

@hikari-no-yume

This comment has been minimized.

hikari-no-yume commented May 18, 2016

No workaround is needed for defining with const, HHVM supports that now.

If you need define(), though, try this:

function defineWorkaroundForHHVM(string $name, $value) /* : void */ {
    $slashPos = strpos($name, '\\');
    $namespace = "";
    if ($slashPos !== false) {
        $namespace = substr($name, 0, $slashPos);
        $name = substr($name, $slashPos + 1);
    }
    $value = var_export($value, true);
    eval('namespace ' . $namespace . '{ const ' . $name . ' = ' . $value . '; }');
}

(You probably shouldn't use this code.)

@Webysther

This comment has been minimized.

Webysther commented May 21, 2016

Sorry @TazeTSchnitzel, I was not clear, is about arrays class constant values.

https://3v4l.org/T0cPe

@paulbiss paulbiss removed their assignment Jun 16, 2016

@Erwyn

This comment has been minimized.

Erwyn commented Jul 21, 2016

It does not work on PHP7 compatibility neither:

hhvm -d hhvm.php7.all=1 whatever.php

result: Arrays are not allowed in class constants

@craigcarnell

This comment has been minimized.

Contributor

craigcarnell commented Aug 4, 2016

Doesn't work for me in latest either

kherge added a commit to Sqon/app that referenced this issue Aug 19, 2016

Working around HHVM not supporting array constants.
HHVM currently does not support for PHP 5.6 constants.
facebook/hhvm#4277
@wulftone

This comment has been minimized.

wulftone commented Oct 17, 2016

Been a few months, this deserves a bump. I would like to use arrays in class constants, too!

kiler129 added a commit to kiler129/ComposerPsr3 that referenced this issue Oct 21, 2016

fisharebest added a commit to fisharebest/webtrees that referenced this issue Jan 5, 2017

ibrahimlawal added a commit to yabacon/paystack-php that referenced this issue Jan 30, 2017

@oakad

This comment has been minimized.

oakad commented Feb 14, 2017

I wonder, why this, absolutely harmful in its absence, feature is still not supported by hhvm after 3 years?

A brief inspection of code indicates, that prohibition of const arrays (either global scope or in-class) even managed to survive the recent refactoring of datatype/variant handling, so it probably was a deliberate decision by the implementors.

However, nowadays massive amounts of php code depend on const arrays and their use is increasing, thus making migration to hhvm outright impossible, despite all the other advantages hhvm may have provided.

@hikari-no-yume

This comment has been minimized.

hikari-no-yume commented Feb 14, 2017

Almost all my new PHP code uses constant arrays somewhere, and therefore probably won't work on HHVM. I doubt I'm alone here, it's a very useful feature.

@Kozzi11

This comment has been minimized.

Kozzi11 commented Feb 14, 2017

I have to refactor all our codebase to remove all const array to try hhvm and after that I realize it is impossible to use latest hhvm on anything else than ubuntu and debian, so this is a bye bye to HHVM OTOH php 7 is even faster in my tests so no harm here :)

@fredemmott

This comment has been minimized.

Contributor

fredemmott commented Feb 14, 2017

@mofarrell @paulbiss bump

Added 'probably easy' given I guess a lot of the required work was already done to support https://github.com/facebook/hhvm/blob/master/hphp/test/quick/vec/static.php

@oakad

This comment has been minimized.

oakad commented Feb 15, 2017

@fredemmott

To my humble understanding fixing this would require a new class derived from Array (something like "StaticArray" with appropriate bounds on values). There's no indication in current code (3.17 that is) that anything was done in this direction.

@fredemmott

This comment has been minimized.

Contributor

fredemmott commented Feb 15, 2017

IIRC that's existed for a long time (used by array literals and APC) - but the plumbing to hook them up to classes didn't exist.

@oakad

This comment has been minimized.

oakad commented Feb 15, 2017

HHVM is a nice tech, especially when it's native api is concerned, but this feature here is a huge blocker. On behalf of the wider user community I urge you to take a look at fixing this. :-)

Tobion added a commit to Tobion/psr7 that referenced this issue Feb 22, 2017

@TysonAndre

This comment has been minimized.

TysonAndre commented Mar 17, 2017

Re: #4277 (comment)

It seems like reviews.facebook.net is no longer a public site (or just the review https://reviews.facebook.net/D29163).

Is it possible to commit the original commits for that PR and link to it (Or just post a patch file of the diff for that commit) in this issue?

@Orvid

This comment has been minimized.

Contributor

Orvid commented Mar 17, 2017

There is work ongoing internally to fix this as part of supporting hack arrays properly.

@fredemmott

This comment has been minimized.

Contributor

fredemmott commented Mar 17, 2017

reviews.facebook.net no longer exists in any form - it's not just no longer public.

@hhvm-bot hhvm-bot closed this in c0f7d16 Mar 21, 2017

@fredemmott

This comment has been minimized.

Contributor

fredemmott commented Mar 21, 2017

@ricklavoie

post-67207-monty-python-and-there-was-muc-vyis

@eryi

This comment has been minimized.

eryi commented Apr 14, 2017

Is this only for PHP? Hack code still gives the same error.

<?hh

class Foo {
  const array<mixed> BAR = [];
}

Tested using 3.19.1

hhvm: Catchable fatal error: Hack type error: Illegal constant value at ...
hh_client: Illegal constant value (Naming[2023])
@fredemmott

This comment has been minimized.

Contributor

fredemmott commented May 2, 2017

HHVM supports it, the typechecker doesn't.

hhvm-bot added a commit that referenced this issue May 6, 2017

support array/varray/darray/vec/keyset/dict constants
Summary:
HHVM supports these now, so hh should as well. This adds support for these types to const declarations; there is nothing preventing them working other than the naming pass in this case. This supports all permutations of nested Hack arrays in any constant position (class constants or global ones).

Some other subtleties to take care of for parity with the runtime are (a) disallowing array-like class constants in a lvalue position and (b) disallow `unset` on array-like constants.

Fixes #4277.

Reviewed By: dabek, alexchow

Differential Revision: D4990753

fbshipit-source-id: 561ea85157682863ca11af3a2040c6b0c7e4c3a7
@fredemmott

This comment has been minimized.

Contributor

fredemmott commented Jun 8, 2017

@wulftone : typechecker now supports it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment