4

We recently had a disaster and had to move our php web application from PHP Version 5.2.6-1+lenny16 to PHP Version 5.3.3-7+squeeze15 and found a seemingly important difference.

In our application, there were instances where we incorrectly called an array's index using object syntax:

echo $array->index;

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

Upon further testing, what 5.2.6 is specifically doing is disagreeing with 5.3.3 as to whether $array->index is empty();

Here is the test code I've run 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 the two different outputs:

5.2.6-1+lenny16
not empty

5.3.3-7+squeeze15
empty

Naturally, there are now a few outbreaks of broken functionality because we were never alerted to these errors during development. Is there a way we can configure php 5.3 to permit this incorrect syntax while we take a bit more time to find all the incorrect instances of it?

I don't think it's a configuration issue, is it? Was something changed in the way empty() works in between versions?

1
  • Are you sure that $array is array? Cannot reproduce your error. Can you provide full example for reproduction? Commented Jun 3, 2013 at 6:54

1 Answer 1

4

I just have put your example code to a general test across PHP versions (test) and it shows that you are correct, there are differences:

From PHP 5.0.0 up to 5.2.11 (and also early 5.3.0 to 5.3.1), this "undefined property" was reported as not empty which does qualify as a flaw or bug.

The related change in 5.2.12 (17 Dec 2009) was (ref):

  • Fixed bug #50255 (isset() and empty() silently casts array to object). (Felipe)

Technically this is not a backwards incompatible change from PHP 5.2 to 5.3 because it was a flaw in both branches and also fixed in both. Those are harder to spot if you migrate, because the standard migration guide does not cover them. Instead you need to go through the changes of the software and look for notes and references to tickets.

So to answer your question: This is a configuration issue because the PHP version used counts as configuration. You changed the configuration and then you had the issue.

Also as the report shows, this is limited to empty() and isset(), not general object/array access. As you can imagine, if that would have been the case, you would have found much more reference about it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.