PHP

Update your Server to PHP5.4+

With many new features and more speed it makes sense to upgrade to PHP5.4.
It is also a requirement when you start using CakePHP3.0 (the dev releases) or code that contains short array syntax.
So the sooner you upgrade the better.

The following examples are for Debian Squeeze.

Backup

Make sure you have a backup of your server/files/db and your php.ini and other config files.

Updating

Debian doesn’t offer this natively.
We need to add some special packages for this.

Open the sources list using nano, vi or the edit tool of your choice:

vi /etc/apt/sources.list

And add those two lines at the end:

deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable all

deb http://packages.dotdeb.org squeeze-php54 all
deb-src http://packages.dotdeb.org squeeze-php54 all

We need a public key to avoid those warnings showing up:

wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key add -

Use apt-get update and apt-get install php5.

That should take care of it. PHP5.4.x should now be installed and ready to work with.
And have fun playing around with CakePHP3.0 or some new features of PHP5.4 🙂

Keep other packages up to date

Also make sure other packages are up to date using apt-get upgrade.

If it doesn’t install right away, one might also need apt-get dist-upgrade. Careful with this, though. It might install/overwrite more than it should.

Dev Libraries

If php5-dev libraries are needed, they can be installed via apt-get install php5-dev.

Key Benefits of 5.4

  • Speed/Stability
  • Traits
  • Closures and $this
  • callable as typehint
  • Short array syntax (no mixin of method brackets and array brackets anymore)

The only real benefit coming in 5.5 is "Generators" which will allow handling of huge arrays and larger amount of data more gracefully.
But other than that 5.4 is just fine for the moment.

Features of 5.3 you can now also use in Cake3

  • Namespaces
  • Closures
  • Late Static Bindings
  • Ternary shorthand form
  • Magic method __callStatic()
  • Nested exceptions

These features weren’t used in Cake2 core (and probably most apps) so far due to the BC for PHP5.2. If you already updated your minimum requirement for your server and application, those
are also now of great use.

Further tips

The encoding even in more current verions of apache usually defaults to the old "ISO-8859-1" one. You should make it "utf-8" per default in your php.ini:

; PHP's default character set is set to empty.
; http://php.net/default-charset
default_charset = "utf-8"

Then even error pages or non-normal responses will be properly displayed. It is sad that even 2014 and in PHP5.4 the default is still not a proper one – probably due to BC or something.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.