3v4l.org

run code in 300+ PHP versions simultaneously
<?php // オブジェクトの不要なプロパティを削除 function removeUnwantedProperties(mixed $element): mixed { if (is_array($element)) { foreach ($element as $key => $val) { if ($key !== "\0*\0" && is_object($val)) { $element[$key] = removeUnwantedProperties($val); } } } elseif (is_object($element)) { $rc = new ReflectionClass($element); $properties = $rc->getProperties(); foreach ($properties as $property) { if (!filterProperties($property)) { unset($element->{$property->getName()}); } else { $getterName = 'get' . ucfirst($property->getName()); if (method_exists($element, $getterName)) { $element->{$property->getName()} = removeUnwantedProperties($element->$getterName()); } else { die('throw'); } } } } return $element; } function filterProperties($property) { $attributes = $property->getAttributes(); foreach ($attributes as $attribute) { if ($attribute->getName() === NoOutput::class) { return false; } } return true; } class Test1 { public $x; // protected だとエラーになる #[NoOutput] public $y; public function __construct() { $this->x = "X"; $this->y = "Y"; } public function getX() { return $this->x; } public function getY() { return $this->y; } } $t1 = new Test1(); $object = removeUnwantedProperties($t1); print_r($object);
Output for git.master, git.master_jit, rfc.property-hooks
Test1 Object ( [x] => X )

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:
78.22 ms | 405 KiB | 5 Q