推 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