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

Readonly Properties ?

pagos
December 12, 2021

Readonly Properties ?

2021/12/17
【PHP8.1リリース記念】PHPerのためのPHP8.1をもっと語り合うPHP TechCafe
https://rakus.connpass.com/event/230475/

pagos

December 12, 2021
Tweet

More Decks by pagos

Other Decks in Technology

Transcript

  1. class User { public function __construct ʢ private string $

    name ʣ { }   public function getName ʢʣɿ string { return $ this- > name ; } } ͜Ε·Ͱ w QSJWBUFͳม਺એݴͱQVCMJDͳHFUUFSͷ૊Έ߹Θͤ w ❌ಡΈऔΓઐ༻ɹ⭕มߋ͕ൃੜ͢ΔՄೳੑͷ͋ΔൣғΛ୯ҰΫϥεʹ
  2. class Test { public readonly string $prop;   public function

    __construct(string $prop) { // ॳظԽ . $this->prop = $prop; } }   $test = new Test(“foobar") ; // ಡΈࠐΈ͸OK . var_dump($test->prop); // string(6) "foobar "   // ஋ͷ্ॻ͖͸NG. ୅ೖ͢Δ஋͕ಉ͔͡Ͳ͏͔͸ؔ܎ແ͍ . $test->prop = "foobar"; // Error: Cannot modify readonly property Test::$pro p 3FBEPOMZ1SPQFSUJFTΛ࢖͏ͱ w Ұ౓͚ͩม਺ͷείʔϓ͔ΒͷॳظԽΛڐՄ ɹɹɹɹɹɹɹɹɹɹ↪︎Ϋϥεͷ֎͔ΒͷॳظԽ͸ڐ͞Εͳ͍
  3. ؔ਺ w 3F fl FDUJPO1SPQFSUZJT3FBE0OMZ ͷ௥Ճ 
 ϓϩύςΟ͕SFBEPOMZ͔൱͔Λ֬ೝ w 3F

    fl FDUJPO1SPQFSUZHFU.PEJ fi FST 
 ͕3F fl FDUJPO1SPQFSUZ*4@3&"%0/-:ϑϥάΛฦ͢Α͏ʹ w 3F fl FDUJPO1SPQFSUZTFU7BMVF 
 Ͱಉ͡είʔϓ͔Β͔͠ॳظԽͰ͖ͳ͍ͱ͍͏੍ݶΛճආ͠ɺॳظԽՄೳ 
 ౰વطʹॳظԽ͞ΕͨϓϩύςΟ͸มߋෆՄ
  4. +BWBͱ1)1ͷൺֱ +BWB 1)1 ޮՌ Ϋϥε fi OBM fi OBM ܧঝͷېࢭ

    ϝιου fi OBM fi OBM ΦʔόϥΠυͷېࢭ ม਺ fi OBM SFBEPOMZ ࠶୅ೖͷېࢭ
  5. ͳΜͰGJOBM͡Όͳ͍ HJUIVCϓϧϦΫΤετͷDPOWFSTBUJPO IUUQTHJUIVCDPNQIQQIQTSDQVMMΑΓ Ωʔϫʔυ͸ fi OBM͕͍͍ͱࢥ͏ʂ +BWBͰ΋࢖ΘΕ͍ͯΔ͠ɺҰ؏ੑͷ؍఺͔Βݟͯ΋ద੾ͩΑͶʂ 1)1ݴޠ։ൃऀ LPDTJTNBUF fi

    OBM͸͋Γಘͳ͍Αɻ 1)1Ͱ͸ݱࡏ fi OBM͸ΫϥεͱϝιουͷΦʔόʔϥΠυΛ๷͙Ωʔϫʔυɻ ʮෆมʯͷҙຯΛ࣋ͭϓϩύςΟʹ࢖༻͢Δͷ͸ؒҧ͍ͩΑɻ
  6. ͳΜͰGJOBM͡Όͳ͍ ࠓճͷ݁࿦ +BWB 1)1 ޮՌ Ϋϥε fi OBM fi OBM

    ܧঝͷېࢭ ϝιου fi OBM fi OBM ΦʔόϥΠυͷېࢭ ม਺ fi OBM SFBEPOMZ ࠶୅ೖͷېࢭ ܧঝΛ๷͙ ஋ͷ࠶มߋΛ๷͙