New errors in PHP 5.4

I’ve been running the PHP5.4 RC builds for the last few months, and there are some interesting changes in the upcoming PHP release. On top of all the great new features coming in PHP5.4. After updating to PHP5.4-RC4, a few things that used to not trigger errors and silently do the wrong thing, now trigger notices or warnings.

Show Plain Text
  1. <?php
  3. // Warning: Illegal string offset 'foo'
  4. $str = 'something';
  5. $str['foo'] = 'bar';
  7. // Notice: String offset cast occured
  8. $i = floor((6 * 4) / 6);
  9. $str = 'abc123';
  10. var_dump($str[$i]);

The sample script above shows the two most recent errors I ran into. I found both tthese errors running the CakePHP testsuite. Interestingly enough the same tests didn’t trigger errors in PHP 5.3 or previous releases of PHP 5.4. Its nice to see things that used to do the ‘wrong thing’ now signalling that behavior. Also good things to keep in mind when upgrading to PHP 5.4.



The previous behavior was the wrong one! PHP is leaning toward a more strict, structured language and those kind of errors/warnings are the example of it.

The first case, does exactly what it already should do and it didn’t, warn you that “Hey this is not an array!”…

About that notice, although in C (and in PHP, ‘cause it’s c-based) the string is an array of chars, in PHP has slightly a different behavior. It does work, as it should, but strings in PHP behave like ‘string’ type, rather than ‘char’. The notice is correct, although more of an info than an ‘error’ itself.

Helps on debugging too…

Ricardo Machado on 1/6/12

Ricardo: I’m all for the new errors. I think fixing the loose and often incorrect behaviour in PHP is a great thing.

mark story on 1/7/12

Now that PHP 5.4 is out, and CakePHP 2.1 is out, will these play safe together.

I’ve only just managed to start doing development in PHP 5.3 (mostly a by product of the production environments they have to be hosted in), but I’m wondering if this would be a golden opportunity to be ahead on the PHP curve, instead of 2 or 3 years behind it.

Reuben on 3/14/12

Reuben: CakePHP 2.x and 1.x work fine on PHP5.4. I’ve been using the various RC’s of 5.4 to make sure of that.

I think the current plan for the next big release of CakePHP is to target 5.3, as its finally getting widespread adoption from hosting environments. While I would like to go 5.4, I think the majority of our community will still be on 5.3. And really if the framework is 5.3, there is nothing preventing developers from using traits, or other 5.4 features in their applications.

mark story on 3/15/12

basic auth doesn’t work on php 5.4… same code on php 5.3.17 works, but doesn’t work on php 5.4. Response is 403 or a404

ddragas on 11/16/12

Comments are not open at this time.