0

i mentioned a strange issue regarding this topic.. I "solved" it myself, but wanted to discuss if anybody understands the problem behind this.

This query works fine with php 7.0:

$image = (ProductImage::all()->where('productHistory_id', $product->history_id))->first();

And causes a syntax error, unexpected '->' (T_OBJECT_OPERATOR).

This query (without the brackets) works fine with php 7.0 and 5.6:

$image = ProductImage::all()->where('productHistory_id', $product->history_id)->first();

whaaaat?!

Kind regards, Nico

MoonMoon
  • 23
  • 4

1 Answers1

0

PHP type checking was eavily revamped between 5.x and 7.x

In both versions the expression:

ProductImage::all()->where('productHistory_id', $product->history_id)

return an instance of a QueryBuilder.

I suspect the presence of the parenthesis cause in earlyer versions of the PHP interpreter to understand it as a scalar value (as in (1+1)+1)) instead of an object value.

This explains why you get an unexpected object operator because PHP 5.X doesn't understand the return of the (...) expression as an object correctly.

This bug is caused exactly by the same parsing error as this one about array dereferencing. It was present on PHP pre 5.4 and was caused by the intereter not detecting the return of a function as an array without using a variable to store it beforehand.

Also on a side note, the parenthesis doesn't change anything as operations on objects are always executed left to right in a statement. May I recommend you to avoid adding useless noise to your codebase?

Community
  • 1
  • 1
Atrakeur
  • 4,126
  • 3
  • 17
  • 22