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

PHP7 傳說中的第七隻大象

PHP7 傳說中的第七隻大象

簡介 PHP7 新特色,並重點提示 PHP7 向下相容以及無法相容的部分,同時回顧一下 PHP 各版本時程。

bobo52310

June 02, 2016
Tweet

More Decks by bobo52310

Other Decks in Programming

Transcript

  1. य़姛 • ࢧ觎稲դ PHP 粚๜ • 㯽藯ӾጱPHP 7 • 7

    殻碝粬ᜋ • 7 讨膏膑粚癩吖蒂 • 7 կࣁ PHP 7 ᥝ螨عጱԪ
  2. • ࢧ觎稲դ PHP 粚๜ • 㯽藯ӾጱPHP 7 • 7 殻碝粬ᜋ

    • 7 讨膏膑粚癩吖蒂 • 7 կࣁ PHP 7 ᥝ螨عጱԪ
  3. PHP 6 ? ࢧ觎稲դ PHP 粚๜ Personal Home Page Tools

    1.0 PHP/FI 2 PHP 3 PHP 4 PHP 5 PHP 7 1995 1997 1998 2000 2004 2015
  4. 匍դ PHP 粚๜ 2013 2014 2015 2016 2017 2018 5.3

    5.4 5.5 5.6 7.0 http://php.net/supported-versions.php 7/10 12/3 12/31 12/3 12/3 12/31 Active support Security fixes only
  5. 匍դ PHP ᯿ᥝ粬ᜋ 5.3 Namespace Anonymous Functions ?: Fix bugs

    5.4 Traits Array [ ] php -S localhost:8000 5.5 Generators ClassName::class try ... catch ... finally list()牏yield 5.6 Constant expressions ¶ Enhance use operator ** Variadic functions via ... ¶ 7.0
  6. • ࢧ觎稲դ PHP 粚๜ • 㯽藯ӾጱPHP 7 • 7 殻碝粬ᜋ

    • 7 讨膏膑粚癩吖蒂 • 7 կࣁ PHP 7 ᥝ螨عጱԪ
  7. 㯽藯ӾጱPHP 7 • 懿䛂誢አᰁๅ੝ • 硳胼൉܋Ӟ狝 • 禅֯ጱݻӥፘ਻௔(?) • ෝ

    2015/12/3 ྋୗ咳蔭 http://media1.santabanta.com/full2/Animals/Elephants/elephants-14a.jpg
  8. • ࢧ觎稲դ PHP 粚๜ • 㯽藯ӾጱPHP 7 • 7 殻碝粬ᜋ

    • 7 讨膏膑粚癩吖蒂 • 7 կࣁ PHP 7 ᥝ螨عጱԪ
  9. 7殻碝粬ᜋ 1. The Null Coalesce Operator (??) 2. The Spaceship

    Operator (<=>) 3. Type Declarations 4. Anonymous Classes 5. Whitelisting Unserialization 6. Session Initialization Options 7. Group use declarations(Namespace use) and more… http://php.net/manual/en/migration70.new-features.php
  10. ই֜ୌ缏 PHP7 䁆ᤈ絑ह牫 • ֵአ MAMP Pro 獥矦 (๜稞 Demo

    ֵአ) • ֵአ phpbrew 㬵獥矦 PHP 犋ݶ粚๜ by c9s • Laravel Homestead
  11. ?? Null Coalesce Operator // PHP 5 if (isset($_GET['user'])) {


    $user = $_GET['user'];
 } else {
 $user = 'Nobody';
 } // PHP 5.3
 $user = isset($_GET['user']) ? $_GET['user'] : 'Nobody';
 
 PHP5 $user = $_GET['user'] ?? 'Nobody'; PHP7
  12. <=> Spaceship Operator echo 1 <=> 2; // -1
 echo

    1 <=> 1; // 0
 echo 2 <=> 1; // 1 https://www.javacodegeeks.com/wp-content/uploads/2014/01/space-ship.jpeg PHP7
  13. <=> Spaceship Operator function comparison($a, $b)
 {
 if ($a ==

    $b) {
 return 0;
 }
 return ($a < $b) ? -1 : 1;
 } PHP5 function comparison($a, $b)
 {
 return $a <=> $b;
 } PHP7
  14. Type Declarations • Scalar type declarations物int牏string牏float牏bool 
 <?php
 
 function

    setAge(int $age)
 {
 
 }
 
 setAge('this is string.');
 
 setAge('30'); // 䨝ᛔ㵕旉ࣳ傶 integer
 PHP7
  15. Type Declarations • Return type declarations <?php
 
 function getUser()

    : User
 {
 return [];
 } interface SomeInterface {
 public function getUser() : User;
 
 } PHP7
  16. Type Declarations • Strict mode <?php
 declare(strict_types=1); // 樄珸盅牧PHP 犋䨝ᛔ㵕旉ࣳ


    
 function setAge(int $age)
 {
 
 }
 
 setAge('this is string.');
 PHP7
  17. use App\{Person, Animal};
 use App\{Person as P, Animal as A};

    Group use declarations use App\Person;
 use App\Animal; // 襑ᥝ㮆獨 use 蝱㬵 PHP5 PHP7
  18. • ࢧ觎稲դ PHP 粚๜ • 㯽藯ӾጱPHP 7 • 7 殻碝粬ᜋ

    • 7 讨膏膑粚癩吖蒂 • 7 կࣁ PHP 7 ᥝ螨عጱԪ
  19. 7 讨膏膑粚癩吖蒂 1. Octal Literals 2. String Parsing 3. Division

    by Zero 4. Uniform Variable Syntax 5. list() Behavior 6. Error Handling // ဳ఺Many fatal errors converted to Exceptions 7. PHP4 style Constructors are not allowed http://php.net/manual/en/migration70.incompatible.php and more…
  20. Octal Literals // ࣁ8蝱ګӥ牧ইຎ磪覍ဩጱਁᒧ牧PHP 䨝盄অஞጱ䒻֦ℂᮎ㮆ਁᒧԏ盅᮷ truncated ധ echo 0128; //䨝䒻֦狕ྋ౮

    012 echo 01281122; //䨝䒻֦狕ྋ౮ 012 PHP5 echo 0128; // ᘒPHP7 㳷ฎ䨝㽆ڊӞ㮆 Parse error: Invalid numeric literal PHP7
  21. String Parsing 
 var_dump(is_numeric("0x01A")); // true
 
 var_dump('0x1A' + '0x01');

    // ፘ吚ෝ10蝱ګጱ 26+1
 PHP5 var_dump(is_numeric("0x01A")); // false
 
 var_dump('0x1A' + '0x01'); // 0+0=0 PHP7
  22. Division by Zero var_dump(5/0); // 疥䨝ࢧ㯽 bool(false) 
 var_dump(0/0); //

    疥䨝ࢧ㯽 bool(false) 
 var_dump(5%0); // 疥䨝ࢧ㯽 bool(false) PHP5 var_dump(5/0); // 疥䨝ࢧ㯽 float(INF)
 var_dump(0/0); // 疥䨝ࢧ㯽 float(NAN) // ޾ JavaScript ᤈ傶觊犲牧JS ࢧ㯽ጱฎ NaN
 var_dump(5%0); // 瞦ڊ DivisionByZeroError ֺक़ PHP7
  23. • ࢧ觎稲դ PHP 粚๜ • 㯽藯ӾጱPHP 7 • 7 殻碝粬ᜋ

    • 7 讨膏膑粚癩吖蒂 • 7 կࣁ PHP 7 ᥝ螨عጱԪ ...蝡犚᮷૪妿ᤩ祌አԧ T_T
  24. 7կࣁPHP7ᥝ螨عጱԪ 1. Never Use PHP Close Tags At the End

    of A File 2. Never Pass By Reference If Not Required 3. mysql_functions Should Not Be Used (ᘒฎ䛑扗አ mysqli_*) 4. ereg PCRE extension: preg_* ૪妿祌አ(ᘒฎ䛑扗ᥝ硬አ PCRE extension: preg_*) 5. PHP4 Constructors Are Now Deprecated 6. Removed Extensions 7. Removed old and unsupported SAPIs