$30 off During Our Annual Pro Sale. View Details »

The new features of PHP 5.6 - Webinar

Joshua Thijssen
September 09, 2014
240

The new features of PHP 5.6 - Webinar

Joshua Thijssen

September 09, 2014
Tweet

Transcript

  1. PHP 5.6 - The new features
    1
    Webinar - 9 sep 2014
    dinsdag 9 september 14

    View Slide

  2. 2
    Joshua Thijssen
    Netherlands
    Consultant and trainer @ TechAdemy
    Founder of TechAnalyze.io
    Author of PHP|Architect's "Mastering the SPL"
    Blog: http://adayinthelifeof.nl
    Email: [email protected]
    Twitter: @jaytaph
    dinsdag 9 september 14

    View Slide

  3. 3
    Offers workshops, training and consultancy:
    PHP
    Symfony2
    Infrastructure + ops
    dinsdag 9 september 14

    View Slide

  4. 4
    Tech nalyze
    Assess technical skills automatically:
    PHP, Symfony2, Scrum
    -
    HTML5/CSS3
    Javascript / JQuery
    dinsdag 9 september 14

    View Slide

  5. PHP 5.6
    5
    http://en.wikipedia.org/wiki/PHP#cite_note-releaseprocess-50
    dinsdag 9 september 14

    View Slide

  6. PHP 5.6
    ➡ PHP 5.3 EOL 14 august 2014
    5
    http://en.wikipedia.org/wiki/PHP#cite_note-releaseprocess-50
    dinsdag 9 september 14

    View Slide

  7. PHP 5.6
    ➡ PHP 5.3 EOL 14 august 2014
    ➡ PHP 5.5 EOL 20 june 2016
    5
    http://en.wikipedia.org/wiki/PHP#cite_note-releaseprocess-50
    dinsdag 9 september 14

    View Slide

  8. PHP 5.6
    ➡ PHP 5.3 EOL 14 august 2014
    ➡ PHP 5.5 EOL 20 june 2016
    ➡ PHP 5.6 EOL 28 august 2017
    5
    http://en.wikipedia.org/wiki/PHP#cite_note-releaseprocess-50
    dinsdag 9 september 14

    View Slide

  9. How to get it?
    ➡ http://blog.famillecollet.com/pages/
    Config-en
    ➡ https://launchpad.net/~ondrej/
    +archive/ubuntu/php5-5.6
    ➡ http://windows.php.net/download/
    ➡ http://php-osx.liip.ch/
    6
    dinsdag 9 september 14

    View Slide

  10. BC breaks?
    7
    dinsdag 9 september 14

    View Slide

  11. BC breaks?
    ➡ minor
    7
    dinsdag 9 september 14

    View Slide

  12. BC breaks?
    ➡ minor
    ➡ SSL Peer verification
    7
    dinsdag 9 september 14

    View Slide

  13. BC breaks?
    ➡ minor
    ➡ SSL Peer verification
    ➡ some array niches
    7
    dinsdag 9 september 14

    View Slide

  14. 1 2
    3 class C
    4 {
    5 const ONE = 1;
    6
    7 public $a = array(
    8 self::ONE => 'foo',
    9 'bar',
    10 'baz',
    11 );
    12 }
    13
    14 $c = new C();
    15 var_dump($c->a);
    8
    PHP 5.5 - 5.6 compatibility break
    dinsdag 9 september 14

    View Slide

  15. 1 2
    3 class C
    4 {
    5 const ONE = 1;
    6
    7 public $a = array(
    8 self::ONE => 'foo',
    9 'bar',
    10 'baz',
    11 );
    12 }
    13
    14 $c = new C();
    15 var_dump($c->a);
    8
    1 array(2) {
    2 [0] =>
    3 string(3) "bar"
    4 [1] =>
    5 string(3) "baz"
    6 }
    php 5.5
    PHP 5.5 - 5.6 compatibility break
    dinsdag 9 september 14

    View Slide

  16. 1 2
    3 class C
    4 {
    5 const ONE = 1;
    6
    7 public $a = array(
    8 self::ONE => 'foo',
    9 'bar',
    10 'baz',
    11 );
    12 }
    13
    14 $c = new C();
    15 var_dump($c->a);
    8
    1 array(2) {
    2 [0] =>
    3 string(3) "bar"
    4 [1] =>
    5 string(3) "baz"
    6 }
    php 5.5
    1 array(3) {
    2 [1]=>
    3 string(3) "foo"
    4 [2]=>
    5 string(3) "bar"
    6 [3]=>
    7 string(3) "baz"
    8 }
    php 5.6
    PHP 5.5 - 5.6 compatibility break
    dinsdag 9 september 14

    View Slide

  17. Upgrading to php5.6
    ➡ From 5.5: just read the migration guide
    ➡ http://php.net/manual/en/
    migration56.php
    ➡ From 5.3: read the 5.4, 5.5 and 5.6
    migration guides.
    9
    dinsdag 9 september 14

    View Slide

  18. 10
    What's new in PHP 5.6?
    ➡ Constant scalar
    expressions
    ➡ Variadic
    functions
    ➡ Argument
    unpacking
    ➡ Exponentation
    ➡ Use function /
    Use const
    ➡ GMP operator
    overloading
    ➡ __debuginfo()
    ➡ SSL peer
    verification
    dinsdag 9 september 14

    View Slide

  19. Constant scalar
    expressions
    11
    dinsdag 9 september 14

    View Slide

  20. 12
    ➡ Use simple expressions to define constants.
    ➡ Evaluated during compile time, not runtime.
    dinsdag 9 september 14

    View Slide

  21. 1 2
    3 class C
    4 {
    5 const BASE_LEVEL = 100;
    6
    7 const CONST_ONE = self::BASE_LEVEL + 1;
    8 const CONST_TWO = self::BASE_LEVEL + 2;
    9 const CONST_THREE = self::BASE_LEVEL + 3;
    10 const CONST_FOUR = self::BASE_LEVEL + 4;
    11 }
    12
    13
    Constant expressions
    dinsdag 9 september 14

    View Slide

  22. 1 2
    3 class C
    4 {
    5 const VALID = 1;
    6 const CHECKED = 2;
    7 const IN_SYNC = 512;
    8 const PRIORITY = 4096;
    9 }
    10
    14
    Cleaner bitwise constants
    dinsdag 9 september 14

    View Slide

  23. 1 2
    3 class C
    4 {
    5 const VALID = 1;
    6 const CHECKED = 2;
    7 const IN_SYNC = 512;
    8 const PRIORITY = 4096;
    9 }
    10
    14
    1 2
    3 class C
    4 {
    5 const VALID = 1 << 0;
    6 const CHECKED = 1 << 1;
    7 const IN_SYNC = 1 << 9;
    8 const PRIORITY = 1 << 12;
    9 }
    Cleaner bitwise constants
    dinsdag 9 september 14

    View Slide

  24. 15
    ➡ You can't use:
    ➡ function calls
    ➡ variables
    dinsdag 9 september 14

    View Slide

  25. 16
    dinsdag 9 september 14

    View Slide

  26. 16
    ➡ But you can use:
    dinsdag 9 september 14

    View Slide

  27. 16
    ➡ But you can use:
    ➡ __FILE__, __CLASS__,
    __NAMESPACE__ etc
    dinsdag 9 september 14

    View Slide

  28. 16
    ➡ But you can use:
    ➡ __FILE__, __CLASS__,
    __NAMESPACE__ etc
    ➡ Constants from other classes
    dinsdag 9 september 14

    View Slide

  29. 16
    ➡ But you can use:
    ➡ __FILE__, __CLASS__,
    __NAMESPACE__ etc
    ➡ Constants from other classes
    ➡ String literals
    dinsdag 9 september 14

    View Slide

  30. 16
    ➡ But you can use:
    ➡ __FILE__, __CLASS__,
    __NAMESPACE__ etc
    ➡ Constants from other classes
    ➡ String literals
    ➡ Even the ternary operator
    dinsdag 9 september 14

    View Slide

  31. 17
    1 2
    3 class C
    4 {
    5 const ONE = 1;
    6
    7 const LINE_ONE = "This is a line " . self::ONE;
    8 }
    Different constant scalar expression examples
    dinsdag 9 september 14

    View Slide

  32. 17
    1 2
    3 class C
    4 {
    5 const ONE = 1;
    6
    7 const LINE_ONE = "This is a line " . self::ONE;
    8 }
    1 ?php
    2
    3 class D
    4 {
    5 const ONE = 1;
    6 }
    7 class C
    8 {
    9 const THREE = D::ONE + 2;
    10 }
    Different constant scalar expression examples
    dinsdag 9 september 14

    View Slide

  33. 1 class C
    2 {
    3 const LOG_LEVEL_HIGH = 105;
    4
    5 const LOG_LEVEL_CRITICAL = self::LOG_LEVEL_HIGH >= 100 ? 250 : 100;
    6 }
    17
    1 2
    3 class C
    4 {
    5 const ONE = 1;
    6
    7 const LINE_ONE = "This is a line " . self::ONE;
    8 }
    1 ?php
    2
    3 class D
    4 {
    5 const ONE = 1;
    6 }
    7 class C
    8 {
    9 const THREE = D::ONE + 2;
    10 }
    Different constant scalar expression examples
    dinsdag 9 september 14

    View Slide

  34. Variadic functions
    18
    dinsdag 9 september 14

    View Slide

  35. 19
    dinsdag 9 september 14

    View Slide

  36. 19
    ➡ It's nothing more than func_get_args()
    dinsdag 9 september 14

    View Slide

  37. 19
    ➡ It's nothing more than func_get_args()
    ➡ Uses '...' before the $argument
    dinsdag 9 september 14

    View Slide

  38. 19
    ➡ It's nothing more than func_get_args()
    ➡ Uses '...' before the $argument
    ➡ More readable
    dinsdag 9 september 14

    View Slide

  39. 19
    ➡ It's nothing more than func_get_args()
    ➡ Uses '...' before the $argument
    ➡ More readable
    ➡ Makes a bit more sense
    dinsdag 9 september 14

    View Slide

  40. 1 2
    3 function foo(... $arguments) {
    4 print_r($arguments);
    5 }
    6
    7 foo(1,2,3,4);
    8
    20
    Using variadic arguments
    dinsdag 9 september 14

    View Slide

  41. 1 2
    3 function foo(... $arguments) {
    4 print_r($arguments);
    5 }
    6
    7 foo(1,2,3,4);
    8
    20
    1 Array
    2 (
    3 [0] => 1
    4 [1] => 2
    5 [2] => 3
    6 [3] => 4
    7 )
    Using variadic arguments
    dinsdag 9 september 14

    View Slide

  42. 1 function findEarliestDate(\DateTime ... $arguments) {
    2 $earliest = null;
    3 foreach ($arguments as $dt) {
    4 if (! $earliest || $dt < $earliest) {
    5 $earliest = $dt;
    6 }
    7 }
    8
    9 return $earliest;
    10 }
    11
    12 $d1 = new DateTime('2014-01-31 10:42:29');
    13 $d2 = new DateTime('2014-01-01 00:00:00');
    14 $d3 = new DateTime('2020-04-01 12:13:41');
    15 $d4 = new DateTime('2010-04-01 12:13:41');
    16 $earliest = findEarliestDate(new DateTime(), $d1, $d2, $d3, $d4);
    21
    Using typehints
    dinsdag 9 september 14

    View Slide

  43. 1 2
    3 function foo($level, $format = "html", ... $arguments) {
    4 print "LEVEL: ".$level."\n";
    5 print "FORMAT: ".$format."\n";
    6 print_r($arguments);
    7 }
    8
    9 foo(1, "text", 3, 4);
    10 foo(1, "text");
    11 foo(1);
    22
    Mixing mandatory, optional and variadic arguments
    Variadic arguments must come last!
    dinsdag 9 september 14

    View Slide

  44. 1 2
    3 function foo(... $args) {
    4 print_r($args);
    5 }
    6
    7 $a = [ 1, "foo" => 2, 3, 4 ];
    8 foo(... $a);
    23
    Cannot use as associative arrays, only numerical arrays
    dinsdag 9 september 14

    View Slide

  45. 1 2
    3 function foo(... $args) {
    4 print_r($args);
    5 }
    6
    7 $a = [ 1, "foo" => 2, 3, 4 ];
    8 foo(... $a);
    23
    Catchable fatal error: Cannot unpack array with string keys
    Cannot use as associative arrays, only numerical arrays
    dinsdag 9 september 14

    View Slide

  46. 1 2
    3 function foo(... $args) {
    4 print_r($args);
    5 }
    6
    7 $a = [ 1, "foo" => 2, 3, 4 ];
    8 foo(... $a);
    23
    Catchable fatal error: Cannot unpack array with string keys
    Cannot use as associative arrays, only numerical arrays
    1 foo(... array_values($a));
    dinsdag 9 september 14

    View Slide

  47. 1 2
    3 function foo(... $args) {
    4 print_r($args);
    5 }
    6
    7 $a = [ 1, "foo" => 2, 3, 4 ];
    8 foo(... $a);
    23
    Catchable fatal error: Cannot unpack array with string keys
    Cannot use as associative arrays, only numerical arrays
    1 foo(... array_values($a));
    Might break a future named arguments feature
    dinsdag 9 september 14

    View Slide

  48. 24
    dinsdag 9 september 14

    View Slide

  49. 24
    ➡ Does it make sense?
    dinsdag 9 september 14

    View Slide

  50. 24
    ➡ Does it make sense?
    ➡ Automatic type hinting (but only one type)
    dinsdag 9 september 14

    View Slide

  51. 24
    ➡ Does it make sense?
    ➡ Automatic type hinting (but only one type)
    ➡ Not so much for dynamic data (arrays), but
    more so for static
    dinsdag 9 september 14

    View Slide

  52. Argument unpacking
    25
    dinsdag 9 september 14

    View Slide

  53. 26
    ➡ Related to variadic functions
    ➡ "unpacks" array or anything that
    implements traversable into variables, just
    like list() does.
    ➡ splat operator
    ➡ Only usable in function calls
    dinsdag 9 september 14

    View Slide

  54. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 foo(1,2,3);
    27
    Simple function call with 3 arguments
    dinsdag 9 september 14

    View Slide

  55. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 foo(1,2,3);
    27
    Simple function call with 3 arguments
    1
    2
    3
    dinsdag 9 september 14

    View Slide

  56. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2, 3 ];
    10
    11 list($tmp1, $tmp2, $tmp3) = $a;
    12 foo($tmp1, $tmp2, $tmp3);
    28
    Using list() to extract elements first
    dinsdag 9 september 14

    View Slide

  57. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2, 3 ];
    10
    11 list($tmp1, $tmp2, $tmp3) = $a;
    12 foo($tmp1, $tmp2, $tmp3);
    28
    Using list() to extract elements first
    1
    2
    3
    dinsdag 9 september 14

    View Slide

  58. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2, 3 ];
    10 foo(... $a);
    29
    PHP 5.6 Argument unpacking
    dinsdag 9 september 14

    View Slide

  59. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2, 3 ];
    10 foo(... $a);
    29
    PHP 5.6 Argument unpacking
    1
    2
    3
    dinsdag 9 september 14

    View Slide

  60. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2, 3, 4, 5 ];
    10 foo(... $a);
    30
    PHP 5.6 Argument unpacking with overflow
    dinsdag 9 september 14

    View Slide

  61. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2, 3, 4, 5 ];
    10 foo(... $a);
    30
    PHP 5.6 Argument unpacking with overflow
    Fills up what needs to be filled, discards the rest
    dinsdag 9 september 14

    View Slide

  62. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2, 3, 4, 5 ];
    10 foo(... $a);
    30
    PHP 5.6 Argument unpacking with overflow
    Fills up what needs to be filled, discards the rest
    1
    2
    3
    dinsdag 9 september 14

    View Slide

  63. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2 ];
    10 foo(... $a);
    31
    PHP 5.6 Argument unpacking with underflow
    dinsdag 9 september 14

    View Slide

  64. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2 ];
    10 foo(... $a);
    31
    PHP 5.6 Argument unpacking with underflow
    Warning, but fills with NULL
    dinsdag 9 september 14

    View Slide

  65. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2 ];
    10 foo(... $a);
    31
    PHP 5.6 Argument unpacking with underflow
    Warning, but fills with NULL
    1
    2
    NULL
    dinsdag 9 september 14

    View Slide

  66. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2 ];
    10 $b = new ArrayIterator([3, 4]);
    11 foo(... $a, ... $b);
    32
    PHP 5.6 Multiple argument unpacks
    dinsdag 9 september 14

    View Slide

  67. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2 ];
    10 $b = new ArrayIterator([3, 4]);
    11 foo(... $a, ... $b);
    32
    PHP 5.6 Multiple argument unpacks
    1
    2
    3
    dinsdag 9 september 14

    View Slide

  68. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2, 3 ];
    10 foo('foo', 'bar', ... $a);
    33
    Mixing argument unpacking with normal arguments
    dinsdag 9 september 14

    View Slide

  69. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2, 3 ];
    10 foo('foo', 'bar', ... $a);
    33
    Mixing argument unpacking with normal arguments
    foo
    bar
    1
    dinsdag 9 september 14

    View Slide

  70. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2 ];
    10 foo(... $a, 'foo', 'bar');
    34
    Mixing argument unpacking with normal arguments
    dinsdag 9 september 14

    View Slide

  71. 1 2
    3 function foo($a, $b, $c) {
    4 print_r($a);
    5 print_r($b);
    6 print_r($c);
    7 }
    8
    9 $a = [ 1, 2 ];
    10 foo(... $a, 'foo', 'bar');
    34
    Mixing argument unpacking with normal arguments
    Fatal error: Cannot use positional argument after argument unpacking
    dinsdag 9 september 14

    View Slide

  72. Exponentation
    35
    dinsdag 9 september 14

    View Slide

  73. 36
    ➡ ** operator (and **= as shorthand)
    ➡ power operator
    dinsdag 9 september 14

    View Slide

  74. 1 2
    3 $a = 2 ** 3;
    4 print $a;
    37
    Power operator
    8
    dinsdag 9 september 14

    View Slide

  75. 38
    ➡ This operator is right associative
    dinsdag 9 september 14

    View Slide

  76. 1 2
    3 $a = -2 ** 3;
    4 print $a . "\n";
    5
    6 $a = -3 ** 2;
    7 print $a . "\n";
    39
    Associativity
    -8 // correct
    -9 // incorrect
    dinsdag 9 september 14

    View Slide

  77. 1 2
    3 $a = (- 2) ** 3;
    4 print $a . "\n";
    5
    6 $a = (- 3) ** 2;
    7 print $a . "\n";
    40
    Associativity
    -8 // correct
    9 // correct
    dinsdag 9 september 14

    View Slide

  78. Use function / const
    41
    dinsdag 9 september 14

    View Slide

  79. 42
    dinsdag 9 september 14

    View Slide

  80. 42
    ➡ Import/alias constants and functions into
    the current namespace.
    dinsdag 9 september 14

    View Slide

  81. 42
    ➡ Import/alias constants and functions into
    the current namespace.
    ➡ PHP 5.3: classes, interfaces, namespaces
    dinsdag 9 september 14

    View Slide

  82. 42
    ➡ Import/alias constants and functions into
    the current namespace.
    ➡ PHP 5.3: classes, interfaces, namespaces
    ➡ PHP 5.6: constants, functions
    dinsdag 9 september 14

    View Slide

  83. 43
    ➡ Useful?
    ➡ IMHO: it's not.
    dinsdag 9 september 14

    View Slide

  84. 44
    1 2
    3 namespace Foo;
    4
    5 function bar($namespace) {
    6 print "This is " . __FUNCTION__ . ", and called from namespace ". $namespace. "\n";
    7 }
    8
    9 namespace Another\Name;
    10
    11 use function Foo\bar;
    12
    13 bar(__NAMESPACE__); // Calls the Foo\bar() method
    Use function
    dinsdag 9 september 14

    View Slide

  85. 44
    1 2
    3 namespace Foo;
    4
    5 function bar($namespace) {
    6 print "This is " . __FUNCTION__ . ", and called from namespace ". $namespace. "\n";
    7 }
    8
    9 namespace Another\Name;
    10
    11 use function Foo\bar;
    12
    13 bar(__NAMESPACE__); // Calls the Foo\bar() method
    Use function
    This is Foo\bar, and called from namespace Another\Name
    dinsdag 9 september 14

    View Slide

  86. 45
    1 2
    3 namespace Foo;
    4
    5 function bar($namespace) {
    6 print "This is " . __FUNCTION__ . ", and called from namespace ". $namespace. "\n";
    7 }
    8
    9 namespace Another\Name;
    10
    11 use function Foo\bar as foobar;
    12
    13 foobar(__NAMESPACE__); // Calls the Foo\bar() method
    Use function with aliasing
    dinsdag 9 september 14

    View Slide

  87. 45
    1 2
    3 namespace Foo;
    4
    5 function bar($namespace) {
    6 print "This is " . __FUNCTION__ . ", and called from namespace ". $namespace. "\n";
    7 }
    8
    9 namespace Another\Name;
    10
    11 use function Foo\bar as foobar;
    12
    13 foobar(__NAMESPACE__); // Calls the Foo\bar() method
    Use function with aliasing
    This is Foo\bar, and called from namespace Another\Name
    dinsdag 9 september 14

    View Slide

  88. 46
    ➡ You can still define the same functions and
    use them in your own namespace
    ➡ Could break things easily
    dinsdag 9 september 14

    View Slide

  89. 47
    1 2
    3 namespace Foo;
    4
    5 function bar($namespace) {
    6 print "This is " . __FUNCTION__ . ", and called from namespace ". $namespace. "\n";
    7 }
    8
    9 namespace Another\Name;
    10
    11 use function Foo\bar;
    12
    13 // Can be defined without any problems
    14 function bar() {
    15 print "Bar!";
    16 }
    17
    18 bar(__NAMESPACE__); // Calls the Foo\bar() method
    19
    20 \Another\Name\bar(); // Calls our own bar() method
    Multiple function defines
    dinsdag 9 september 14

    View Slide

  90. 47
    1 2
    3 namespace Foo;
    4
    5 function bar($namespace) {
    6 print "This is " . __FUNCTION__ . ", and called from namespace ". $namespace. "\n";
    7 }
    8
    9 namespace Another\Name;
    10
    11 use function Foo\bar;
    12
    13 // Can be defined without any problems
    14 function bar() {
    15 print "Bar!";
    16 }
    17
    18 bar(__NAMESPACE__); // Calls the Foo\bar() method
    19
    20 \Another\Name\bar(); // Calls our own bar() method
    Multiple function defines
    This is Foo\bar, and called from namespace Another\Name
    Bar!
    dinsdag 9 september 14

    View Slide

  91. ➡ Just like regular functions, it automatically
    falls back to the global namespace when the
    function is not found.
    ➡ RFC says otherwise
    48
    dinsdag 9 september 14

    View Slide

  92. 49
    1 2
    3 // Foo namespace
    4 namespace Foo {
    5
    6 function strlen($str)
    7 {
    8 return "The length of the string is: " . \strlen($str) . "\n";
    9 }
    10
    11 }
    12
    13
    14 // Another\Name namespace
    15 namespace Another\Name {
    16
    17 use function Foo\strlen;
    18
    19 print strlen(__NAMESPACE__); // Calls \Foo\strlen() function
    20
    21 }
    Fallbacks
    dinsdag 9 september 14

    View Slide

  93. 50
    1 2
    3 // Foo namespace
    4 namespace Foo {
    5
    6 function strlen($str)
    7 {
    8 return "The length of the string is: " . \strlen($str) . "\n";
    9 }
    10
    11 }
    12
    13
    14 // Another\Name namespace
    15 namespace Another\Name {
    16
    17 print strlen(__NAMESPACE__); // Falls back to the global strlen() function
    18
    19 }
    Fallbacks
    dinsdag 9 september 14

    View Slide

  94. 51
    1 2
    3 // Global namespace
    4 namespace {
    5
    6 function strlength($str)
    7 {
    8 return "Hi there. This is the global strlength() function: " . strlen($str) . "\n";
    9 }
    10
    11 }
    12
    13 // Foo namespace
    14 namespace Foo {
    15
    16 function strlength($str)
    17 {
    18 return "The length of the string is: " . \strlen($str) . "\n";
    19 }
    20
    21 }
    22
    23
    24 // Another\Name namespace
    25 namespace Another\Name {
    26
    27 print strlength(__NAMESPACE__); // Falls back to the the \strlength() method
    28
    29 }
    Automatic fallback to global namespace
    dinsdag 9 september 14

    View Slide

  95. GMP Overloading
    52
    dinsdag 9 september 14

    View Slide

  96. 53
    ➡ New concept in PHP:
    Operator overloading
    ➡ Only GMP extension
    ➡ More to follow, most likely
    dinsdag 9 september 14

    View Slide

  97. 1 2
    3 $a = 1 + 2;
    4 var_dump($a); // int(3)
    5
    6 $a = 1 + 1.1;
    7 var_dump($a); // float(2.1)
    8
    9 $a = "foo" + 1;
    10 var_dump($a); // int(1)
    11
    12 $a = 1 + "foo";
    13 var_dump($a); // int(1)
    14
    15 $a = new Color("yellow") + new Color("blue");
    16 var_dump($a); // Color("green") ???
    54
    PHP "acts" differently based on operands given
    dinsdag 9 september 14

    View Slide

  98. 1 2
    3 $a = gmp_init(1) + gmp_init(5);
    4 var_dump($a);
    55
    object(GMP)#3 (1) {
    ["num"]=>
    string(1) "6"
    }
    GMP "overloads" the operators
    dinsdag 9 september 14

    View Slide

  99. 1 2
    3 $a = gmp_init(1) + 2;
    4 var_dump($a);
    56
    object(GMP)#3 (1) {
    ["num"]=>
    string(1) "3"
    }
    GMP "overloads" the operators
    dinsdag 9 september 14

    View Slide

  100. 1 2
    3 $a = gmp_init(1) + 2;
    4 var_dump($a); // GMP(3)
    5
    6 $a = 8 + gmp_init(1);
    7 var_dump($a); // GMP(9)
    8
    9 $a = 8.6 + gmp_init(1);
    10 var_dump($a); // Unable to convert variable to GMP
    11
    12 $a = true + gmp_init(1);
    13 var_dump($a); // GMP(2)
    14
    15 $a = false + gmp_init(1);
    16 var_dump($a); // GMP(1)
    57
    GMP can convert only INT and BOOL, not FLOAT :(
    dinsdag 9 september 14

    View Slide

  101. 58
    ➡ GMP operator overloading is not meant to
    start userland overloading (opr_add, coerce
    etc)
    ➡ It's just to make GMP usage easier
    dinsdag 9 september 14

    View Slide

  102. 1 $result = gmp_mod(
    2 gmp_add(
    3 gmp_mul($c0, gmp_mul($ms0, gmp_invert($ms0, $n0))),
    4 gmp_add(
    5 gmp_mul($c1, gmp_mul($ms1, gmp_invert($ms1, $n1))),
    6 gmp_mul($c2, gmp_mul($ms2, gmp_invert($ms2, $n2)))
    7 )
    8 ),
    9 gmp_mul($n0, gmp_mul($n1, $n2))
    10 );
    11
    12 $result = (
    13 $c0 * $ms0 * gmp_invert($ms0, $n0)
    14 + $c1 * $ms1 * gmp_invert($ms1, $n1)
    15 + $c2 * $ms2 * gmp_invert($ms2, $n2)
    16 ) % ($n0 * $n1 * $n2);
    59
    Clearer way to deal with GMP numbers
    https://wiki.php.net/rfc/operator_overloading_gmp
    dinsdag 9 september 14

    View Slide

  103. __debuginfo
    60
    dinsdag 9 september 14

    View Slide

  104. 61
    ➡ New magic method
    ➡ Allows to control the output of var_dump()
    dinsdag 9 september 14

    View Slide

  105. 1 2
    3 class Foo {
    4 protected $file;
    5
    6 function __construct($path = null) {
    7 if ($path != null) {
    8 $this->file = fopen($path, "r");
    9 }
    10 }
    11
    12 }
    13
    14 $foo = new Foo("/etc/passwd");
    15 print_r($foo);
    62
    Simple class with file resource
    Foo Object
    (
    [file:protected] => Resource id #5
    )
    dinsdag 9 september 14

    View Slide

  106. 1 class Foo {
    2 protected $file;
    3
    4 function __construct($path = null) {
    5 if ($path != null) {
    6 $this->file = fopen($path, "r");
    7 }
    8 }
    9
    10 function __debuginfo() {
    11 $debuginfo = array();
    12
    13 $debuginfo['opened'] = $this->file ? "true" : "false";
    14
    15 if ($this->file) {
    16 $debuginfo['file'] = stream_get_meta_data($this->file);
    17 }
    18 return $debuginfo;
    19 }
    20
    21 }
    22
    23 $foo = new Foo("/etc/passwd");
    24 print_r($foo);
    25
    26 $bar = new Foo();
    27 print_r($bar);
    63
    Simple class with file resource and __debuginfo
    dinsdag 9 september 14

    View Slide

  107. 1 class Foo {
    2 protected $file;
    3
    4 function __construct($path = null) {
    5 if ($path != null) {
    6 $this->file = fopen($path, "r");
    7 }
    8 }
    9
    10 function __debuginfo() {
    11 $debuginfo = array();
    12
    13 $debuginfo['opened'] = $this->file ? "true" : "false";
    14
    15 if ($this->file) {
    16 $debuginfo['file'] = stream_get_meta_data($this->file);
    17 }
    18 return $debuginfo;
    19 }
    20
    21 }
    22
    23 $foo = new Foo("/etc/passwd");
    24 print_r($foo);
    25
    26 $bar = new Foo();
    27 print_r($bar);
    63
    Simple class with file resource and __debuginfo
    Foo Object
    (
    [opened] => true
    [file] => Array
    (
    [wrapper_type] => plainfile
    [stream_type] => STDIO
    [mode] => r
    [unread_bytes] => 0
    [seekable] => 1
    [uri] => /etc/passwd
    [timed_out] =>
    [blocked] => 1
    [eof] =>
    )
    )
    Foo Object
    (
    [opened] => false
    )
    dinsdag 9 september 14

    View Slide

  108. SSL Peer verification
    64
    dinsdag 9 september 14

    View Slide

  109. 65
    ➡ https://wiki.php.net/rfc/operator_overloading_gmp
    ➡ http://php.net/manual/en/migration56.php
    ➡ http://php.net/manual/en/migration56.new-features.php
    ➡ https://wiki.php.net/rfc/const_scalar_exprs
    ➡ https://wiki.php.net/rfc/variadics
    Further reading materials:
    ➡ http://phpdancer.blogspot.nl/p/php-wallpapers.html
    dinsdag 9 september 14

    View Slide

  110. 66
    Find me on twitter: @jaytaph
    Find me for development and training: www.noxlogic.nl
    Find me on email: [email protected]
    Find me for blogs: www.adayinthelifeof.nl
    Thanks for your attention
    https://joind.in/event/view/2584
    Please leave feedback at
    dinsdag 9 september 14

    View Slide