Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Overview changes in PHP 5.4

505 views

Published on

Some things changed from little thing to big stuff.

Published in: Technology
  • Be the first to comment

Overview changes in PHP 5.4

  1. 1. PHP 5.4 Vo Xuan Tien
  2. 2. The little things • • • • • Array Dereferencing Short Open Tag Short Array Syntax Binary number format Class member access on instantiation
  3. 3. Array Dereferencing (PHP < 5.4) • $variables = getVariables(); • $second_variable = $variables[1];
  4. 4. Array Dereferencing (PHP >= 5.4) • $second_variable = getVariables()[1]; • explode('.', 'menu.main_test')[0]; • use with ArrayAccess interface (see demo)
  5. 5. Short Open Echo Tag • <?= $hello_world ?> • <?= "May be not work on shared host" ?>
  6. 6. Short Array Syntax • $a = [1, 2, 3, 4,]; • $a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4,]; • $a = [["nested 1", "nested 2"], "xyz"];
  7. 7. 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)
  8. 8. PHP < 5.4 • • • • • • • { $temp = new Class(); do_something($temp); // forgot to unset($temp); do_other_thing(); ... }
  9. 9. Class member access on instantiation • do_something(new Foo); • do_something((new Foo)->bar());
  10. 10. The little things • • • • • • JSON Serialization Helper Native Session Handler Interface Objects as Functions Callable Type-Hint $this in Anonymous Functions Initialized High Precision Timer
  11. 11. JSON Serialization Helper • class A implements sonSerializable • public function jsonSerialize() • echo json_encode (new A());
  12. 12. Native Session Handler Interface (PHP < 5.4) • session_set_save_handler($open, $close, $read, $write, $destroy, $gc);
  13. 13. Native Session Handler Interface (PHP >= 5.4) • class A implements SessionHandlerInterface • session_set_save_handler(new A);
  14. 14. Objects as Functions __invoke • function __invoke() • $instance = new A(); • echo $instance();
  15. 15. Callable Type-Hint • • • • • • function call_me(callable $function); anonymous function non-anonymous function static method instance method callable object (see previous slide)
  16. 16. $this in Anonymous Functions (Closures) • function () { $this->test(); }; • more fun in demo
  17. 17. Initialized High Precision Timer (PHP < 5.4) • $start = microtime(1); • /* your code here */ • echo "took: ", (microtime(1) $start);
  18. 18. Initialized High Precision Timer (PHP >= 5.4) • /* your code here */ • echo "took: ", (microtime(1) $_SERVER['REQUEST_TIME_FL OAT']); • This fairly useless
  19. 19. The big stuffs • Traits (Horizontal reuse, Multiple inheritance) • "use" operator • Built-in CLI Web-Server • Performance Improvements
  20. 20. Trait • Demo
  21. 21. "use" operator • "use" for namespace • "user" for closure • "use" for trait
  22. 22. Built-in CLI Web-Server • php -S localhost:8080 • php -S localhost:8080 -t /var/www/web • php -S localhost:8080 router.php
  23. 23. Performance Improvements • Real-life 5-20% faster • Benchmarks 15-20% faster • Memory 25% reduced
  24. 24. Thank You Tien Vo Xuan References http://ilia.ws/files/confoo_php54.pdf @tienvoxuan tien.voxuan

×