3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CanCastBool extends SimpleXMLElement { /** @var WeakMap 各 CanCastBool インスタンスのプロパティを保持する WeakMap */ public static WeakMap $properties; /** * XML を意識しないコンストラクタ * @param string $fuga * @return CanCastBool */ public static function makeInstance(string $fuga): CanCastBool { $instance = new self('<bool></bool>'); $instance->true = true; self::$properties ??= new WeakMap(); // WeakMap に自分をキーにしてプロパティを記録させる // WeakMap に退避させておくので XML とは別の扱いになる self::$properties[$instance] = [ 'fuga' => $fuga, ]; return $instance; } /** プロパティ的に読み取るメソッド。WeakMap から取ってくる */ public function getP(string $name) { return self::$properties[$this][$name]; } /** プロパティ的にセットするメソッド。WeakMap にセットする */ public function setP($name, $value) { self::$properties[$this][$name] = $value; } public function toTrue(): void { $this->true = true; } public function toFalse(): void { unset($this->true); } } $a = CanCastBool::makeInstance('fugafuga'); $b = CanCastBool::makeInstance('piyopiyo'); echo "---init----\n"; var_dump((bool)$a);// true var_dump($a->getP('fuga'));// 'fugafuga' $a->setP('fuga', 'foo'); $a->setP('hoge', 'bar'); echo "----------\n"; $a->toFalse(); var_dump((bool)$a);// false var_dump($a->getP('fuga'));// 'foo' var_dump($a->getP('hoge'));// 'bar' $a->setP('hoge', 'hogehoge'); echo "----------\n"; $a->toTrue(); var_dump((bool)$a);// true var_dump($a->getP('fuga'));// 'foo' var_dump($a->getP('hoge'));// 'hogehoge' var_dump($b->getP('fuga'));// 'piyopiyo'
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.4, 8.3.6 - 8.3.14
---init---- bool(true) string(8) "fugafuga" ---------- bool(false) string(3) "foo" string(3) "bar" ---------- bool(true) string(3) "foo" string(8) "hogehoge" string(8) "piyopiyo"
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 ---init---- bool(true) string(8) "fugafuga" ---------- bool(false) string(3) "foo" string(3) "bar" ---------- bool(true) string(3) "foo" string(8) "hogehoge" string(8) "piyopiyo"
Output for 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Class 'WeakMap' not found in /in/kBnZJ:18 Stack trace: #0 /in/kBnZJ(51): CanCastBool::makeInstance('fugafuga') #1 {main} thrown in /in/kBnZJ on line 18
Process exited with code 255.
Output for 7.3.0 - 7.3.33
Parse error: syntax error, unexpected 'WeakMap' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/kBnZJ on line 6
Process exited with code 255.

preferences:
73.94 ms | 410 KiB | 5 Q