PHP in 2013
Adam Harvey
@LGnome
Where we were
Time And Relative Dimensions In Space
TARDIS by Tama Leaver (CC-BY 2.0)
2009
The noted documentary District 9
District 9, by Sony Pictures
PHP 5.3
PHP 5.3
PHP 5.3
Yeah, we should have called it 6.0
I have no idea what I'm doing
thefrogman, argumentinvalid, zNoisha, twlehew
Lifecycle
2010
Double rainbow all the way?
Montréal - Never seen a double rainbow by Serge Melki (CC-BY 2.0)
A weird ASCII art Gantt chart
Release process
Release process
2012
A man thrusts erotically above another man in a lift. It's a metaphor for the futility of higher instincts in the presence of primal urges.
Gangnam Style, by PSY
PHP 5.4
Where we are: 2013
Duh, we're at OSCON
DSC_1027 by Benjamin Kerensa (CC-BY-SA 2.0)
PHP 5.3
What is the matrix?
PHP 5.3
PHP 5.4
PHP 5.5
New features
A motorcycle with two sidecars and zero safety
stoye seitenwagen
Generators
Generators
Generators
class RangeIterator implements Iterator { private $start; private $limit; private $step; private $current; public function __construct($start, $limit, $step = 1) { $this->start = $start; $this->limit = $limit; $this->step = $step; $this->current = $start; } public function current() { return $this->current; } public function key() { return ($this->current - $this->start) / $this->step; } public function next() { $this->current += $this->step; } public function rewind() { $this->current = $this->start; } public function valid() { return ($this->current >= $this->start && $this->current <= $this->limit); } } function xrange($start, $limit, $step = 1) { return new RangeIterator($start, $limit, $step); }
Generators
function xrange($start, $limit, $step = 1) { for ($i = $start; $i <= $limit; $i += $step) { yield $i; } }
Generators
function xrange($start, $limit, $step = 1) { for ($i = $start; $i <= $limit; $i += $step) { yield $i; } } foreach (xrange(1, 9, 2) as $number) { echo "$number\n"; }
1
3
5
7
9
Finally, finally
Finally, finally
function do_something() { $temp = tempnam('/tmp', 'foo'); try { ... unlink($temp); } catch (FrameworkException $e) { ... unlink($temp); } catch (Exception $e) { unlink($temp); throw $e; } }
Finally, finally
class TemporaryFile { function __construct($directory, $prefix) { $this->file = tempnam($directory, $prefix); } function __destruct() { unlink($this->file); } function __toString() { return $this->file; } } function do_something() { $temp = new TemporaryFile('/tmp, 'foo'); try { ... } catch (FrameworkException $e) { ... } }
Finally, finally
function do_something() { $temp = tempnam('/tmp', 'FOO'); try { ... } catch (FrameworkException $e) { ... } finally { unlink($temp); } }
Finally, finally
function f() { try { return 'try'; } finally { return 'finally'; } } echo f();
finally
Password hashing
Password hashing
echo crypt('foo', '$2a$01234567890123456789a');
$2zJyhpjk3l9E
Password hashing
password_hash($password, $algo, $options) password_verify($password, $hash) password_get_info($hash) password_needs_rehash($hash, $algo, $options)
Password hashing
// Creating a hash: $hash = password_hash($passwd, PASSWORD_DEFAULT); // Verifying a password: if (password_verify($passwd, $hash)) { // do stuff }
Password hashing
Password hashing
OPcache
OPcache
OPcache
OPcache
zend_extension=${extension_dir}/opcache.so
OPcache
5.4 5.5 5.5 + OPcache
php.net 308.73 315.77 681.76
sf 2.1 based 10.65 10.92 21.40
WordPress 3.5.2 23.72 24.93 79.52
Requests per second
mod_php on Apache 2.4
foreach unpacking
$array = [[1, 2], [3, 4]]; foreach ($array as $item) { list($a, $b) = $item; // ... }
foreach unpacking
$array = [[1, 2], [3, 4]]; foreach ($array as list($a, $b)) { // ... }
empty()
empty(my_function());
Literal dereferencing
var_dump([1, 2, 3][0]); var_dump('PHP'[0]);
Literal dereferencing
var_dump([1, 2, 3][0]); var_dump('PHP'[0]); 1 P
::class
namespace Name\Space; class ClassName {} echo ClassName::class;
::class
namespace Name\Space; class ClassName {} echo ClassName::class; Name\Space\ClassName
Immutable DateTime
Backward incompatibilities
A car reverses; crumple zones are required now; no-one is happy
car accident.jpg by Jeremy Noble (CC-BY 2.0)
Windows support
Case insensitivity
i = I
I = i
i = İ
I = ı
$dbh->lastInsertId();
$dbh->lastinsertid();
$dbh->lastınsertıd();
Case insensitivity
Case insensitivity
function función() {} FUNCIÓN();
pack() and unpack()
Deprecated features
Suraxanı. Say that ten times fast
Oil Well in Suraxanı by me (WTFPL)
mysql_connect()
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
The Scream, by Munch
Scream by Christopher Macsurak (CC-BY 2.0)
(The original is by Edvard Munch, obviously)
Use PDO
mysql → PDO
$db = mysql_connect('localhost', 'user', 'pass'); mysql_select_db('db', $db); $id = mysql_real_escape_string($_GET['id'], $db); $st = mysql_query("SELECT name FROM user WHERE id = $id"); while ($row = mysql_fetch_assoc($st)) { echo "Hi, $row[name]!"; }
mysql → PDO
$db = mysql_connect('localhost', 'user', 'pass'); mysql_select_db('db', $db); $id = mysql_real_escape_string($_GET['id'], $db); $st = mysql_query("SELECT name FROM user WHERE id = $id"); while ($row = mysql_fetch_assoc($st)) { echo "Hi, $row[name]!"; }
mysql → PDO
$db = new PDO('mysql:host=localhost;dbname=db', 'user', 'pass'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $st = $db->prepare('SELECT name FROM user WHERE id = :id'); $st->execute(['id' => $_GET['id']]); while ($row = $st->fetch(PDO::FETCH_ASSOC)) { echo "Hi, $row[name]!"; }
preg_replace() /e modifier
preg_replace( '(<h([1-6])>(.*?)</h\1>)e', '"<h$1>" . strtoupper("$2") . "</h$1>"', $html );
preg_replace() /e modifier
preg_replace_callback( '(<h([1-6])>(.*?)</h\1>)', eval('"<h$1>" . strtoupper("$2") . "</h$1>"'), $html )
preg_replace() /e modifier
preg_replace_callback( '(<h([1-6])>(.*?)</h\1>)', eval('"<h$1>" . strtoupper("$2") . "</h$1>"'), $html )
preg_replace() /e modifier
preg_replace_callback( '(<h([1-6])>(.*?)</h\1>)', function (array $matches) { return "<h$matches[1]>" .strtoupper($matches[2]) ."</h$matches[1]>"; }, $html )
That's awesome, but how do I get this?
WANT
Who wants to be a millionaire? by Roo Reynolds (CC-BY 2.0)
Getting PHP 5.5
Getting PHP 5.5
Getting PHP 5.5
Getting PHP 5.5
PuPHPet
Questions?