Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP 7 Feature

PHP 7 Feature

Brief Description about PHP 7 Feature and Changelog

Naing Lin Aung

July 26, 2015
Tweet

More Decks by Naing Lin Aung

Other Decks in Programming

Transcript

  1. PHP 7 huh ? Where did PHP 6 went ???

    • PHP 6 is failure • to prevent confusion • to let sleeping dogs lie
  2. What’re the changes? • Use JSOND instead of JSON •

    Deprecation of PHP 4-Style Constructors • No more `<?=` and <script language=“php"> • Removal of Dead Server APIs • Removal of Hex Support in Numerical Strings • Removal of Deprecated Functionality • Deprecation of Salt Option for password_hash()
  3. What’re the Features? • Double Arrow Operators • Null Coalesce

    Operator • Scalar Type Declarations • Anonymous Classes • Closure call() Method • Group use Declarations • Support for Array Constants in define()
  4. Double Arrow Operator - CCO (Combined Comparison Operator) - combination

    of >,< and = - 1 <=> 2 <=> 3 is not allowed - Objects are not comparable // compares strings lexically var_dump('PHP' <=> 'Node'); // int(1) // compares numbers by size var_dump(123 <=> 456); // int(-1) // compares corresponding array elements with one-another var_dump(['a', 'b'] <=> ['a', 'b']); // int(0)
  5. Null Coalesce Operator - used widely in Swift - very

    useful for shorthand - alternative to ‘||’ in javascript - less isset() $route = isset($_GET['route']) ? $_GET['route'] : 'index'; $route = $_GET['route'] ?? 'index'; PHP 7 Before PHP 7
  6. Scalar Type Declarations Corecive Mode function sumOfInts(int …$ints) { return

    array_sum($ints); } var_dump(sumOfInts(2, '3', 4.1)); // int(9) declare(strict_types=1); function multiply(float $x, float $y) { return $x * $y; } function add(int $x, int $y) { return $x + $y; } var_dump(multiply(2, 3.5)); // float(7) var_dump(add('2', 3)); // Fatal error: Uncaught TypeError: Argument 1 passed to add() must be of the type integer, string given... Strict Mode
  7. Return Type Declarations function arraysSum(array ...$arrays): array { return array_map(function(array

    $array): int { return array_sum($array); }, $arrays); } print_r(arraysSum([1,2,3], [4,5,6], [7,8,9])); /* Output Array ( [0] => 6 [1] => 15 [2] => 24 ) */ PHP 7 Output
  8. $util->setLogger(new class { public function log($msg) { echo $msg; }

    }); class Logger { public function log($msg) { echo $msg; } } $util->setLogger(new Logger()); Anonymous Classes Before PHP 7 PHP 7
  9. use some\namespace\{ClassA, ClassB, ClassC as C}; use function some\namespace\{fn_a, fn_b,

    fn_c}; use const some\namespace\{ConstA, ConstB, ConstC}; use some\namespace\ClassA; use some\namespace\ClassB; use some\namespace\ClassC as C; use function some\namespace\fn_a; use function some\namespace\fn_b; use function some\namespace\fn_c; use const some\namespace\ConstA; use const some\namespace\ConstB; use const some\namespace\ConstC; PHP 7 Before PHP 7 Group `use` Declarations
  10. define('ALLOWED_IMAGE_EXTENSIONS', ['jpg', 'jpeg', 'gif', 'png']); Array Constant Declaration var_dump(is_numeric('0x123')); var_dump('0x123'

    == '291'); echo '0x123' + '0x123'; bool(false) bool(false) 0 bool(true) bool(true) 582 PHP 7 Results Before PHP 7 Results Removal of Hex in String
  11. Fin