PHP 5.4
Vo Xuan Tien
The little things
•
•
•
•
•

Array Dereferencing
Short Open Tag
Short Array Syntax
Binary number format
Class member access on
instantiation
Array Dereferencing (PHP <
5.4)
• $variables = getVariables();
• $second_variable = $variables[1];
Array Dereferencing (PHP >=
5.4)
• $second_variable =
getVariables()[1];
• explode('.', 'menu.main_test')[0];
• use with ArrayAccess interface (see
demo)
Short Open Echo Tag
• <?= $hello_world ?>
• <?= "May be not work on shared
host" ?>
Short Array Syntax
• $a = [1, 2, 3, 4,];
• $a = ['one' => 1, 'two' => 2, 'three'
=> 3, 'four' => 4,];
• $a = [["nested 1", "nested 2"],
"xyz"];
Binary number format
• $a = 1234; // decimal number
• $a = -123; // a negative number
• $a = 0123; // octal number
(equivalent to 83 decimal)
• $a = 0x1A; // hexadecimal number
(equivalent to 26 decimal)
• $a = 0b11111111; // binary number
(equivalent to 255 decimal)
PHP < 5.4
•
•
•
•
•
•
•

{
$temp = new Class();
do_something($temp);
// forgot to unset($temp);
do_other_thing();
...
}
Class member access on
instantiation
• do_something(new Foo);
• do_something((new Foo)->bar());
The little things
•
•
•
•
•
•

JSON Serialization Helper
Native Session Handler Interface
Objects as Functions
Callable Type-Hint
$this in Anonymous Functions
Initialized High Precision Timer
JSON Serialization Helper
• class A implements sonSerializable
• public function jsonSerialize()
• echo json_encode (new A());
Native Session Handler
Interface (PHP < 5.4)
• session_set_save_handler($open,
$close, $read, $write, $destroy,
$gc);
Native Session Handler
Interface (PHP >= 5.4)
• class A implements
SessionHandlerInterface
• session_set_save_handler(new A);
Objects as Functions __invoke
• function __invoke()
• $instance = new A();
• echo $instance();
Callable Type-Hint
•
•
•
•
•
•

function call_me(callable $function);
anonymous function
non-anonymous function
static method
instance method
callable object (see previous slide)
$this in Anonymous Functions
(Closures)
• function () { $this->test(); };
• more fun in demo
Initialized High Precision Timer
(PHP < 5.4)
• $start = microtime(1);
• /* your code here */
• echo "took: ", (microtime(1) $start);
Initialized High Precision Timer
(PHP >= 5.4)
• /* your code here */
• echo "took: ", (microtime(1) $_SERVER['REQUEST_TIME_FL
OAT']);
• This fairly useless
The big stuffs
• Traits (Horizontal reuse, Multiple
inheritance)
• "use" operator
• Built-in CLI Web-Server
• Performance Improvements
Trait
• Demo
"use" operator
• "use" for namespace
• "user" for closure
• "use" for trait
Built-in CLI Web-Server
• php -S localhost:8080
• php -S localhost:8080 -t
/var/www/web
• php -S localhost:8080 router.php
Performance Improvements
• Real-life 5-20% faster
• Benchmarks 15-20% faster
• Memory 25% reduced
Thank You

Tien Vo Xuan
References http://ilia.ws/files/confoo_php54.pdf

@tienvoxuan
tien.voxuan

Overview changes in PHP 5.4