看板 PHP 關於我們 聯絡資訊
https://www.php.net/releases/7_4_0.php 列出一部分變更: - 效能提升(這快要算不上新消息了...) - 物件的成員可以設定資料型別 - public static iterable $list; - 不能用 callable(行為不固定)跟 void(這麼設感覺意義不明) - 箭頭函式(跟 JS 的不太一樣) - 請參照 RFC https://wiki.php.net/rfc/arrow_functions_v2 - 可以在陣列表示式裡面用 spread 運算子 - $ary = ['x', 'y', ...$other, 'z']; - $ary = [...$a, ...$b]; - 可以少寫一些 array_merge(),不過 array_merge 還是有自己的天空 - FFI,簡單說就是可以從 PHP 呼叫 C 的程式。 - 文件 https://www.php.net/manual/en/class.ffi.php - RFC https://wiki.php.net/rfc/ffi - 以前有人做 PHP 的 TensorFlow binding 作為 PoC 火力展示。 - deprecate 一堆...早就不該這麼用的東西 - https://www.php.net/manual/en/migration74.deprecated.php - 比較值得一提的是沒有括號的巢狀三元運算子被 deprecated - $a = 1 ? 2 : 3 ? 4 : 5; // 以後不能這樣 - $a = (1 ? 2 : 3) ? 4 : 5; // 可以這樣 - $a = 1 ? 2 : (3 ? 4 : 5); // 這樣也行 詳細內容請參照: - https://www.php.net/manual/en/migration74.new-features.php - https://github.com/php/php-src/blob/PHP-7.4/UPGRADING -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.122.206 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/PHP/M.1575013359.A.D6B.html
MOONRAKER: @_O 11/29 16:03
dream0405: 期待php8的釋出會變成什麼樣子~~~ 11/29 22:23
gpmm: 感覺 7.4 又是個升級門檻 XDD 11/29 22:33
gpmm: 語法變動這麼頻繁老人家快要跟不上了 11/29 22:33
GALINE: 倒是還好,舊的寫法原則上都可以動。那份 deprecation 表 11/30 16:06
GALINE: 也是眼界大開,「原來PHP(曾經)能這樣亂搞喔」 11/30 16:07
※ 編輯: GALINE (223.139.152.108 臺灣), 11/30/2019 16:11:01
GALINE: 是說試著在一些私人專案用 PHP 自己當樣版引擎(復古風~) 11/30 16:17
GALINE: 加一些簡單的 helper 函式(例如 escape)之後還滿好用的 11/30 16:18
MOONRAKER: 更上次沒看到沒有括號的三元運算子 11/30 18:33
MOONRAKER: 公司有一個已離職的同事最愛寫ternary op連發 11/30 18:34
MOONRAKER: 不時發現這種整人code 一bit blame發現又是他 >:( 11/30 18:34
MangoTW: 類別成員型別推一個 12/02 19:18
newton2009: 借問一下喔 Unbinding $this when $this is used 這段 12/04 19:49
newton2009: 有例子可以說明嗎?我不太能理解這段要表達的意思! 12/04 19:49
你可以在物件裡面做出 closure,然後 $this 會自動綁定到原本的物件 class A { function makeClosure() { return function() { echo "Closure 有跑" . PHP_EOL; var_dump($this); }; } } $func1 = $a->makeClosure(); $func1(); // Closure 有跑 // /private/tmp/a.php:7: // class A#1 (0) { // } 然後你可以用 bindTo() 這個成員方法複製出新的 closure 但 $this 指到另一個物件 $func2 = $func1->bindTo(new stdClass); $func2(); // Closure 有跑 // /private/tmp/a.php:7: // class stdClass#3 (0) { // } 然後你現在還可以 bindTo(null) code 還是能動,只是一用到 $this 就會噴掉 $func3 = $func1->bindTo(null); $func3(); // Closure 有跑 // PHP Fatal error: Uncaught Error: Using $this when // not in object context in /private/tmp/a.php:7 PHP 7.4 之後對原本有 $this 的 $closure 呼叫 bindTo(null) 會直接噴掉 不過 RFC 說明看起來目的比較不是保護開發者不小心搞死自己 而是為了 PHP 8 鋪路 https://wiki.php.net/rfc/deprecations_php_7_4 我一下是想不到為什麼會想要去 bindTo(null) 不過剛剛找 RFC 的時候看到 Laravel / Carbon 還真的踩中這個 https://github.com/laravel/framework/issues/29411 ※ 編輯: GALINE (36.237.100.25 臺灣), 12/05/2019 00:41:50 ※ 編輯: GALINE (36.237.100.25 臺灣), 12/05/2019 09:52:36 ※ 編輯: GALINE (60.248.122.206 臺灣), 12/16/2019 11:33:18