1

I have a problem with in_array using newly introduced array class constants. When I put a class constant which contains an array into the function I get a warning:

Warning: in_array() expects parameter 2 to be array, unknown given in...

Code:

foreach ($fields as $key => $value) {
    if (in_array($key, self::FIELDS)) $this->$key = $value;
}

Constant (inside a class):

const FIELDS = [
    self::FIELD_ID,
    self::FIELD_STREET,
    self::FIELD_HOUSE_NR,
    self::FIELD_POSTCODE,
    self::FIELD_CITY,
    self::FIELD_PERSONAL_NUMBER,
    self::FIELD_SELLER_NAME,
    'empty'
];

The weirdest part of this problem is that it works on my local machine without any errors/warnings (Mac) running PHP 5.6.2 but does not on my server also running PHP 5.6.2.

How do I resolve this error? (I don't want to use static arrays...)

Benjamin Schmidt
  • 1,051
  • 13
  • 29

2 Answers2

0

The root problem is class constants can't be arrays (unless it's php 5.6 or greater as pointed out by bluefirex). PHP Constants Containing Arrays? http://php.net/manual/en/language.oop5.constants.php

Community
  • 1
  • 1
David Fairbanks
  • 638
  • 7
  • 17
0

On PHP 7.2 and in_array returned FALSE with a class constant array, at least in my case...

I used this as a workaround:

if(isset(self::MAPPING_OLD_REFS[$sku]))  $sku = self::MAPPING_OLD_REFS[$sku];
Thony
  • 2,300
  • 1
  • 20
  • 20