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 git.master_jit, git.master, rfc.property-hooks
---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"

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
35.6 ms | 405 KiB | 5 Q