PHP 5.2 allowed object syntax to call array index?

Recently, we ran into a disaster and forced to port our php web application from PHP version 5.2.6-1 + lenny16 to PHP version 5.3.3-7 + squeeze15 and found an apparent significant difference.

In our application, there were cases when we incorrectly called the array index using the object syntax:

echo $array->index; 

However, 5.2.6 seemed to forgive it and relate to it as if $ array ['index'] was written.

In further testing, what 5.2.6 specifically does is inconsistent with 5.3.3 as to whether $ array-> index is empty ();

Here is the test code that I ran on both servers:

 <?php echo phpversion() . '<br>'; $array = array( 'x' => 1, 'y' => 2 ); if (!empty($array->x)) { echo "not empty"; } else { echo "empty"; } ?> 

Here are two different options:

 5.2.6-1+lenny16 not empty 5.3.3-7+squeeze15 empty 

Naturally, there are several outbreaks of broken functionality, because we were never warned about these errors during development. Is there a way that we can configure php 5.3 to resolve this incorrect syntax while we take a bit more time to find all of its incorrect instances?

I don't think this is a configuration problem, right? Has anything changed in the way empty () works between versions?

+4
source share
1 answer

I just put your sample code in a general test through PHP versions ( test ), and it shows that you are right, there are differences

From PHP 5.0.0 to 5.2.11 (and also from early 5.3.0 to 5.3.1) this undefined property was reported as not empty, which is considered a defect or error.

The corresponding change in 5.2.12 (December 17, 2009) was ( ref ):

  • Fixed bug # 50255 ( isset() and empty() silently drops an array on an object). (Felipe)

Technically, this is not a backward incompatible change from PHP 5.2 to 5.3, because it was a flaw in both branches, and also fixed in both. This is harder to detect if you are migrating because the standard migration guide does not apply to them. Instead, you need to go through software changes and see notes and ticket links.

So, to answer your question: this is a configuration problem, because the version of PHP used is considered configuration. You changed the configuration and you have a problem.

Also, as the report shows, this is limited to empty() and isset() , not shared access to objects / arrays. As you can imagine, if that were the case, you would find many more links about it.

+4
source

Source: https://habr.com/ru/post/1484132/


All Articles