"ãã¸ãã¯ã¡ã½ããã¯ã¡ã½ããã ããå¼ã°ããã°åã"件ã«ã¤ãã¦
å
ãã¿ã¯ã https://speakerdeck.com/twada/growing-reliable-code-phperkaigi-2022?slide=84 ãè¦ã¦ãã³ã³ã¹ãã©ã¯ã¿ã2ååãã®ï¼ï¼ãã£ã¦è³ªåããã£ãã®ã§ã
端çã«ã¯ãPHPã«ããã¦ã³ã³ã¹ãã©ã¯ã¿ã¯ãã¸ãã¯ã¡ã½ããã§ããã¸ãã¯ã¡ã½ããã¯ã¡ã½ããã ãããcallããã°åããã£ã¦ããã®ã身ãèããªãåçã«ãªãã¾ãã
ãã¿ç ãã¦ã
ã¾ãPHPã«ãããããã¸ãã¯ã¡ã½ãããã¯ã以ä¸ã®éãã
https://www.php.net/manual/ja/language.oop5.magic.php
ãã¸ãã¯ã¡ã½ããã¯ã ããåä½ããªãã¸ã§ã¯ãã«å¯¾ãã¦è¡ãããå ´åã«ã PHP ã®ããã©ã«ãã®åä½ãä¸æ¸ãããç¹å¥ãªã¡ã½ããã§ãã
"ããåä½ããªãã¸ã§ã¯ãã«å¯¾ãã¦è¡ãããå ´å"ãããªã¬ã¼ã«"PHP ã®ããã©ã«ãã®åä½ãä¸æ¸ãããç¹å¥ãªã¡ã½ãã"ããªã®ã§ãåºæ¬ãã¡ã½ãããã§ãã
ãã§ãPHPã®ã³ã³ã¹ãã©ã¯ã¿ã¯
以ä¸ã®é¢æ°åã¯ããã¸ãã¯ã¡ã½ããã¨è¦ãªããã¾ã: __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __serialize(), __unserialize(), __toString(), __invoke(), __set_state(), __clone(), __debugInfo()
ã¨ããããã«ããã¸ãã¯ã¡ã½ããã§ãã
ãªã®ã§ãPHPã®æåã¨ãã¦ã¯
ã»ã¤ã³ã¹ã¿ã³ã¹ãä½ã
ãâ "ã¤ã³ã¹ã¿ã³ã¹ãä½ã"åä½ããªãã¸ã§ã¯ãã«å¯¾ãã¦è¡ãããå ´åã PHPã®ããã©ã«ãã®åä½ãä¸æ¸ããã __construct() ã¡ã½ããããããªããããã¤ãå®è¡ãã
ã£ã¦ãªæãã«ãªãã¾ãã
ãªã®ã§ã¾ããããããã³ã³ã¹ãã©ã¯ãæã®åä½ã __construct() ã¡ã½ããã«æ¸ãã¦ããã°ãã¤ã³ã¹ã¿ã³ã¹ãçæãããæã«ãæ示çã«ã¯å¼ãã§ãªããã©ãèªåã§callãããããã§ããã
ãã§ã
ã¨ã¯ããã¾ã__construct()ããããã¡ã½ããããªã®ã§ã
ãæ示çã«callããããããããã¯ã¾ããå¼ã°ããããåãããããªãã§ããã
ãããã¦ã¾ããå ãã¿ã®DateTimeImmutableã§ãã³ã³ã¹ãã©ã¯ã¿ãæ示çã«å©ãã¨ã³ã³ã¹ãã©ã¯ã¿ãå¼ã°ãã(ã®ã§ãå¤ãæ¸ãæãããããã£ã¦ã®ã¯ãã¾ããã§ãããï½ããããã®æåã«ãªãã¾ãã
ãã¯ã¦ä»è¨èªã¯ã©ããªã£ã¦ããã ãï¼ãã¨æãããªãã¨ãããã£ã¨ç®ã«æ¸ããC++ã(å¹¾åæãåºãã¤ã¤)æ¸ãã¦ã¿ãã®ã§ããã
#include <iostream> class Hoge { public: // ã³ã³ã¹ãã©ã¯ã¿ Hoge() { std::cout << "construct" << std::endl; } }; int main() { Hoge *h = new Hoge(); h->Hoge(); return 0; }
ããããããã³ã³ã¹ãã©ã¯ã¿ã¯ã³ã³ã¹ãã©ã¯ã¿ã§ãã£ã¦ãã¯ã©ã¹é¢æ°ã§ã¯ãªãããã ããªãã
ãªã®ã§ã¾ã
t.cpp: é¢æ° âint main()â å :
t.cpp:13:8: ã¨ã©ã¼: ç¡å¹ãª âHoge::Hogeâ ã®ä½¿ç¨ã§ã
h->Hoge();
^
ã£ã¦è¨ãããã§ãã ããããã
â¦â¦ãã以å¤ã®è¨èªã¯ã©ï½ãªãã ããããï¼*1
話ãæ»ãã¦ã
ãªã®ã§ã¾ããPHPã®ã³ã³ã¹ãã©ã¯ã¿ã¯æè©®ãã¡ã½ããããªã®ã§ããå¼ã¹ã°åããã§ãã¾ãã
ãä»äºã§èããå ´åããã®è¾ºã¯ãæ示çã«å¼ã¶ãªãã§çµãã£ã¦ãããããªæ°ããããã ããªãâ¦â¦ãªãã¦ããããããã¾ã§é
æ
®ãã«ããªããã®ãç¾å ´ã§ä½¿ãããããï¼ãã¨ããæ£ç´ã
ãã ã¾ããæèå®é¨ã¨ãã¦ãã§ããã°ãä¾ãã°ãããªæããã
<?php declare(strict_types=1); error_reporting(-1); class Hoge { public function __construct() { // ã³ã³ã¹ãã©ã¯ã¿äºécallã®ããã¯æ©æ§ static $double_call_lock = []; $object_id = spl_object_id($this); if (null !== ($double_call_lock[$object_id] ?? null)) { throw new \Exception('ã³ã³ã¹ãã©ã¯ã¿æ示çã«å¼ã¶ã¨ãããã¾ããæ£æ°ãï¼'); } $double_call_lock[$object_id] = true; } } // $obj = new Hoge(); $obj2 = new Hoge(); $obj3 = new Hoge(); //$obj->__construct();
ãã ããã(spl_object_id)ãããã®ãªãã¸ã§ã¯ãidã¯ãªãã¸ã§ã¯ããçãã¦ããéã¦ãã¼ã¯ããªã®ã§ã
// $obj = new Hoge(); $obj = new Hoge(); $obj = new Hoge();
ã£ã¦ããã¨
[gallu@ik1-111-11111 ~]$ php t.php
int(1)
int(2)
int(1)Fatal error: Uncaught Exception: ã³ã³ã¹ãã©ã¯ã¿æ示çã«å¼ã¶ã¨ãããã¾ããæ£æ°ãï¼ in /home/gallu/t.php:14
Stack trace:
#0 /home/gallu/t.php(23): Hoge->__construct()
#1 {main}
thrown in /home/gallu/t.php on line 14
ã£ã¦ãªæãã«ãªãäºããããã§ãããã¾ãåå
¨ã«å¤§ä¸å¤«ã§ããªãã
ããã¾ããã£ã¡ããã¨ãã¹ãã©ã¯ã¿ã¾ã§æ¸ãã¦
<?php declare(strict_types=1); error_reporting(-1); class Hoge { public function __construct() { // ã³ã³ã¹ãã©ã¯ã¿äºécallã®ããã¯æ©æ§ $object_id = spl_object_id($this); var_dump($object_id); if (null !== ($this::$double_call_lock[$object_id] ?? null)) { throw new \Exception('ã³ã³ã¹ãã©ã¯ã¿æ示çã«å¼ã¶ã¨ãããã¾ããæ£æ°ãï¼'); } $this::$double_call_lock[$object_id] = true; } public function __destruct() { // ãã³ã³ã¹ãã©ã¯ã¿äºécallã®ããã¯æ©æ§ãã®è§£é¤ $object_id = spl_object_id($this); echo "unset {$object_id}\n"; unset($this::$double_call_lock[$object_id]); } private static $double_call_lock = []; } // $obj = new Hoge(); $obj = new Hoge(); $obj = new Hoge(); //$obj->__construct();
ã£ã¦ããã°å¤å
[gallu@ik1-111-11111 ~]$ php t.php
int(1)
int(2)
unset 1
int(1)
unset 2
unset 1
ã£ã¦ãªå ·åã«ã¯ãªããã ãããã©ããããã¾ã§æ¸ãï¼ãã£ã¦è©±ã«ãªãããã ãããã
ãªã®ã§ã¾ããã³ã³ã¹ãã©ã¯ã¿ã§å¤ãè¨å®ããã®ã¤ã³ã¹ã¿ã³ã¹ã®å¤ã¯ä¸å¤ãã£ã¦ããã®ã¯ãPHPã®å ´åããç¨åº¦ãåæã¨è¯èãã«å®ããã¦ããæããããã ãããªããã¨ã
ãã ã¾ãããã®è¾ºã横ç´ã«ç ´ãã¾ãããããªå¾¡æ¹ãã¯ãã¡ãã£ã¨â¦â¦ã£ã¦æãã®ã§ã
ãã®è¾ºã¯ã¾ããã©ã¼ãã¼ã¹ã¿ã³ã¹ãæã¤ãã次第ãªããããªãããªãï¼ ã¨ãæããã ãã©ãã©ï½ãªãã ããããï¼ï¼ï¼
*1:ã³ã¡ã³ãã¨ãã§ããã ããã°è¿½è¨ãããã¾ãã