2

I have a code base that I am trying to migrate to 7.0.

In the code, I have something like: isset($user['details']['phone_no']) where $user is an object of User. This works fine in php5.6. where as in php7 it returns false. when doing var_dump($user['details']['phone_no']) it is returning a string.

It works fine when I change the code from:

$phone_no = isset($user['details']['phone_no']) ? $user['details']['phone_no'] : 'XXXXXXXXXX'; 

to:

$phone_no = $user->details->phone_no ?? 'XXXXXXXXXX';

However, the codebase is too huge to refactor.

mx0
  • 6,445
  • 12
  • 49
  • 54
Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34
  • If you're trying to access object properties using array syntax, perhaps you could modify your classes so that they implement [ArrayAccess](http://php.net/manual/en/class.arrayaccess.php). That might be a smaller task than modifying all the uses of accessing objects as though they were arrays – Mark Baker Nov 14 '17 at 11:39
  • 3
    Wouldn't this code already throw an error about not being able to access an object as array? – billyonecan Nov 14 '17 at 11:41
  • No, It's working fine. BTW, User is a model. :P – Sahith Vibudhi Nov 14 '17 at 11:46
  • I would think it already shouldn't work as this is the 1st time I see an `array` syntax used with an `Object`. Anyway, the [null coalescing operator](http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op) is a pretty good improvement that might be used while migrating to PHP 7. **Any more informations about your syntax ?** – AymDev Nov 14 '17 at 11:46
  • It's not really relevant that User is a model, it's relevant that it's a Class tat can be instantiated to objects – Mark Baker Nov 14 '17 at 11:48
  • But billy is right.... if it was working before, then it should still be working, but it's not something that works as standard with any version of PHP – Mark Baker Nov 14 '17 at 11:49
  • I found [ArrayObject](http://php.net/manual/en/class.arrayobject.php), which I don't know. Is it related ? But it is written *PHP5, PHP7* – AymDev Nov 14 '17 at 11:57
  • 1
    ArrayObject is an interface that allows you to access object properties using array syntax, you just have to provide the [implementation](https://3v4l.org/MajRB). It works perfectly well with PHP5 and PHP7; though if you're working with objects, it's far better to work with object syntax – Mark Baker Nov 14 '17 at 12:01
  • debugged, the base model the class is extending from implements `IteratorAggregate` and `ArrayAccess`. – Sahith Vibudhi Nov 14 '17 at 12:07
  • Check your class's implementation of [`ArrayAccess::offsetExists`](http://php.net/manual/en/arrayaccess.offsetexists.php). That's what's being invoked when calling `isset()`, it seems that, in your case, it's returning true when running 5.6 and false when running 7 – billyonecan Nov 14 '17 at 13:07
  • it does this. `public function offsetExists($offset){ return property_exists($this,$offset); }` – Sahith Vibudhi Nov 14 '17 at 14:51
  • what about the implementation of `ArrayAccess::offsetGet`? Please could you edit and add that (and the implementation of `ArrayAccess::offsetExists`) to your question? – billyonecan Nov 14 '17 at 20:12
  • btw that's a bad implementation of `offsetExists`. `isset()` checks that a variable is set, and is not null. This implementation only checks whether the property exists (is set), it will return true if it's null – billyonecan Nov 14 '17 at 20:20
  • @billyonecan well, the offsetExists implementation is from the Yii framework's CModel class. :P – Sahith Vibudhi Nov 16 '17 at 14:24
  • It should be reported as a bug, but it isn't related to the issue you're having. What is `details` an object of? Here's a simple example of a class which implements `ArrayAccess` idential to Yii, you can see it returns true under both 5.6 and 7 (you'll notice that `offsetExists` isn't invoked on `details` under 5.6, but is on 7), this may be the root cause of your problem (it was a bug which was fixed and shipped with PHP7 - https://bugs.php.net/bug.php?id=69659) – billyonecan Nov 16 '17 at 15:28
  • Forgot to link the example - https://3v4l.org/oGd8r – billyonecan Nov 16 '17 at 16:22

1 Answers1

0

Isset behaviour is different in PHP7

https://stackoverflow.com/questions/50171262/strange-behaviour-on-isset-in-php-7?noredirect=1#comment87361765_50171262

http://php.net/manual/en/function.isset.php#51113

Velaro
  • 461
  • 1
  • 3
  • 20