Namespaces (5.3)
Namespaces are for combining libraries in one project.
• we keep our own code in its own namespace(s)
• we can pick-and-mix other people's code
• class names don't have crazy long prefixes
• combined with PSR standards, they enable
Composer
Slide 8
Slide 8 text
Namespaced Library File
namespace Lorna;
class Nonsense {
protected $words =
array("wibble", "squeak", "howl", "pop");
public function speak() {
return $this->words[
array_rand($this->words)];
}
}
Slide 9
Slide 9 text
Namespaced Calling Code
include 'lorna/nonsense.php';
use Lorna\Nonsense;
$nsense = new Nonsense();
echo $nsense->speak();
$other = new \StdClass();
Slide 10
Slide 10 text
Register Globals is Gone (5.4)
Upgrade PHP and see many undefined (simple)
variables.
To fix each undefined variable:
• replace $username with $_GET['username']
or $_POST['username'] in each case
Error Reporting: E_STRICT
E_ALL includes E_STRICT in PHP 5.4+
You can either:
1. Fix your errors
2. Turn it off again using E_ALL & ~E_STRICT
Slide 14
Slide 14 text
Timezone Settings (5.4)
PHP refuses to guess the timezone and will warn you.
It is not safe to rely on the system's timezone settin
You are *required* to use the date.timezone setting or
date_default_timezone_set() function.
In case you used any of those methods and you are stil
warning, you most likely misspelled the timezone ident
Fix it by setting date.timezone in php.ini:
date.timezone = "Europe/London"
Slide 15
Slide 15 text
Call Time Pass By Reference
PHP Fatal error: Call-time pass-by-reference has been
This error says "don't pass references in when the
function didn't expect them, you fool"
Slide 16
Slide 16 text
Call Time Pass By Reference
You can pass by reference. Declare function:
1 function inspire(&$person) {
2 // be inspiring
3 }
Call with just $ and not &
1 inspire($current_user);
Built-in OpCache (5.5)
Replaces APC - beware this is disabled by default.
Turn on opcache.enable and opcache.enable_cli
Slide 20
Slide 20 text
Get Ready for New PHP
Slide 21
Slide 21 text
PHP Peformance
Slide 22
Slide 22 text
Use PHPCodeSniffer
Use the PHPCompatibility PHPCS standard
Install from
https://github.com/wimg/PHPCompatibility and
include in your standards directory
Slide 23
Slide 23 text
PHP Knows What's Deprecated
Turn on E_DEPRECATED on an older version to see
what's removed in the next version
Set with: error_reporting = E_ALL
Slide 24
Slide 24 text
Lint Check
Syntax check your code with new version of PHP
Use phing (http://www.phing.info/):
Or try my one-liner:
find . -name '*.php' | xargs -n1 php -l
Slide 25
Slide 25 text
Serve with PHP
Run application with PHP's webserver (5.4+)
php -S localhost:8080
Use Continuous Integration
Builds
Use something like http://travis-ci.com to run regular
builds
This can run your build steps for various versions of
PHP each time you commit
(it's free for open source users)
Slide 28
Slide 28 text
Check Your Code Is Ready
• Use the PHPCompatibility PHPCS standard
• Turn on E_DEPRECATED and watch the logs of
your existing platform
• Lint check with new version (php -l)
• Run application with PHP's webserver