PHP7 - A new beginning

PHP7 - A new beginning

35617c1bde6460e06755ffada7d55ee8?s=128

Bart Van den Brande

December 17, 2015
Tweet

Transcript

  1. 4.

    Scalar constants - Define constants as an array const THREE

    = [1, 2]; - Modifications in definition const ONE = 5; const TWO = ONE * 2;
  2. 5.

    Var-length arguments - Give any number of arguments - Available

    as an array function sum(...$numbers) { $acc = 0; foreach ($numbers as $n) { $acc += $n; } return $acc; } echo sum(1, 2, 3, 4);
  3. 6.

    - Possibility to use a specific function or a constant

    use const Name\Space\FOO; use function Name\Space\f; ‘use’ function and constant
  4. 7.

    Exponentiation 2 ** 3 = 8 - Use ** as

    a mathematical operator Question 2 ** 3 ** 2 = ?
  5. 11.

    - Released on 4th december 2015 - Support for an

    extended time (jan 2019) - Speed increase, memory decrease - Developers are happy Brand new toys 01 02 03 04 05 07
  6. 12.

    PHP version life cycle 5.3 5.4 5.5 5.6 jan 2012

    - jun 2013 mar 2012 - jul 2014 jun 2013 - jul 2015 aug 2014 - aug 2016
  7. 13.

    Speed increase 100% Thanks to the new Zend Engine 3.0,

    your apps see up to 2x faster performance and 50% better memory consumption than PHP 5.6, allowing you to serve more concurrent users without adding any hardware. Designed and refactored for today’s workloads, PHP 7 is the ultimate choice for web developers today.
  8. 14.

    PHP7 PHP 5.6 ab -c 1 -n 100 http://phpfive.****.n*****.be/wordpress/ ab

    -c 1 -n 100 http://phpseven.****.n*****.be/wordpress/ Live demo ab test
  9. 17.

    01 02 03 04 05 06 Made more simple 01

    Removal of Deprecated Functionality 02 Removal of Alternative PHP Tags 03 Removal of date.timezone Warning 04 PHP4 Constructor deprecated in favour of __construct() 05 JSON Extension Replaced with JSOND 06 Removal of Multiple Default Blocks in Switch Statements
  10. 18.

    Reserved keywords Globally reserved words as property, constant, and method

    names within classes, interfaces, and traits are now allowed. This reduces the surface of BC breaks when new keywords are introduced and avoids naming restrictions on APIs. // 'new', 'private', and 'for' were previously unusable Project::new('Project Name’)->private() ->for('purpose here’) ->with('username here');
  11. 19.

    Reserved keywords There are some new globally reserved keywords. These

    are mostly a result of the scalar typehint addition in PHP7. - int - float - bool - string - true - false - null - resource - object - mixed - numeric
  12. 20.

    Spaceship <=> operator The spaceship operator or combined comparison
 operator,

    compares 2 values and returns a given result. These results can be -1, 0 or 1. var_dump(123 <=> 456); // int(-1) Returns a integer 1 - left hand operator 0 - equal -1 - right hand operator Able to compare - strings - numbers - arrays
  13. 21.

    Null Coalesce Operator The null coalesce operator is a shorthand

    for the isset() function. Developers had to write a lot of similar if statements so the community decided it would be wise to implement a short way to remove complexity in your code. if (isset($_GET[‘route’])) { $route = $_GET[‘route']; } else { $route = ‘index’; } $route = isset($_GET['route']) ? $_GET['route'] : 'index'; $route = $_GET['route'] ?? 'index';
  14. 22.

    Scalar Type Declarations One of the complaints of other developers

    is that PHP is to loose. This gives margin for errors in your code. Scalar type declarations try to handle this issue. function sumOfInts(int ...$ints) { return array_sum($ints); } var_dump(sumOfInts(2, '3', 4.1)); // int(9) declare(strict_types=1); Coercive mode VS Strict mode
  15. 23.

    Return Type Declarations One of the complaints of other developers

    is that PHP is to loose. This gives margin for errors in your code. Scalar type declarations try to handle this issue. function sumOfInts(int …$ints): array { return array_sum($ints); }
  16. 24.

    var_dump( intdiv(10, 3) ); // int(3) Integer devision - Divide

    any integer by an other integer - Automatic rounding
  17. 25.

    define( 'ALLOWED_IMAGE_EXTENSIONS', ['jpg', 'jpeg', 'gif', ‘png'] ); Definition arrays Just

    as constants, definitions are now allowed to have arrays. This wil reduce the amount of code to write when defining application parameters.
  18. 26.

    use some\namespace\ClassA; use some\namespace\ClassB; use some\namespace\ClassC as C; use some\namespace\

    becomes use some\namespace\{ClassA, ClassB, ClassC as C}; Group use declarations If you have a large scale project, your code will be separated in multiple classes. Using these classes will result in lots of ‘use’ statements at the top of your class. PHP7 allows you to group these use statements.
  19. 27.

    Exceptions In PHP 7, an exception will be thrown when

    a fatal and recoverable error (E_ERROR and E_RECOVERABLE_ERROR) occurs, rather than halting script execution. Fatal errors still exist for certain conditions, such as running out of memory, and still behave as before by immediately halting script execution. An uncaught exception will also continue to be a fatal error in PHP 7. This means if an exception thrown from an error that was fatal in PHP 5.x goes uncaught, it will still be a fatal error in PHP 7. interface Throwable |- Exception class implements Throwable |- ... |- Error class implements Throwable |- TypeError extends Error |- ParseError extends Error |- AssertionError extends Error |- ArithmeticError extends Error |- DivisionByZeroError extends ArithmeticError