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 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
Test1 Object ( [x] => X )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
97.48 ms | 407 KiB | 5 Q