0

I've a php script that runs ok on my local (MAC High Sierra 10.13.6, php version 7.1.16) using php file.php ...

hhvm --version gives HipHop VM 3.11.0 (rel)

When I run from vagrant (hhvm), it complains Fatal error: Arrays are not allowed in class constants in certainfile.php on line xxx. using hhvm file.php ....

In my file, I've a const defined as:

class Lalala { const AB_C = array(A::a, B::b,...); function xyz {...use self::AB_C ...} }

I tried to change it to define("AB_C", array(A::a, B::b,...));, but it couldn't even compile with syntax error.

How should I declare the constant arrays here please? Thank you.


define("AB_C", serialize(array(A::a, B::b,...)));also failed with same syntax error

define("AB_C", jsonencode(array(A::a, B::b,...)));also failed with same syntax error

user2751691
  • 401
  • 2
  • 10
  • 32
  • Possible duplicate of [PHP Constants Containing Arrays?](https://stackoverflow.com/questions/1290318/php-constants-containing-arrays) – RisingSun Feb 04 '19 at 18:40
  • HHVM seems to be more restrictive. Is there any reason you have to use HHVM? I got away from HHVM since PHP7. – Pinke Helga Feb 04 '19 at 18:47
  • `define("AB_C", serialize(array(A::a, B::b,...)));` cannot work since `serialize` is a runtime function but HHVM is compiled. You would need preprocessing to evaluate this expression running PHP. Same with `jsonencode` – Pinke Helga Feb 04 '19 at 18:55

1 Answers1

3

You can't use arrays as class constants until I believe 3.19, I know it works as of HHVM 3.19.2 but could have been a little bit earlier. If you update HHVM to the latest (or at least a more recent) version, it will work.

https://hhvm.com/blog/2017/04/13/hhvm-3-19.html

Noteworthy changes include:

Const Array support.

Community
  • 1
  • 1
dave
  • 62,300
  • 5
  • 72
  • 93
  • 3.11 https://3v4l.org/jE8Et edit: ye you right :) 3.19 ... brain lag :D – SirPilan Feb 04 '19 at 18:49
  • Corollary: please don't use old and unsupported versions of HHVM. Version 3.11 is several years old -- I'm not sure how you even got a copy. https://docs.hhvm.com/hhvm/installation/release-schedule details the release schedule and supported versions. – Josh Watzman Feb 05 '19 at 23:36