3v4l.org

run code in 300+ PHP versions simultaneously
<?php function hasImplicitNullDefaultValue(\ReflectionProperty $reflection): bool { if (null !== $reflection->getDefaultValue()) { return false; } if (false === $reflection->getDocComment()) { return true; } $docComment = $reflection->getDocComment(); if (!preg_match('/@var\s+([^\s]+)/', $docComment, $matches)) { return true; } return false !== strpos(strtolower($matches[1]), 'null'); } class foo { public $foo; /** * @var int */ public $bar; /** * @var int|null */ public $baz; } echo '$foo' . PHP_EOL; echo 'Implicit null set. As we do not know otherwise null is fine' . PHP_EOL; $reflection = new ReflectionProperty('foo', 'foo'); var_Dump($reflection->hasDefaultValue()); var_Dump($reflection->getDefaultValue()); var_Dump(hasImplicitNullDefaultValue($reflection)); echo PHP_EOL . '$bar' . PHP_EOL; echo 'null can not be an implicit value as this should be an int' . PHP_EOL; $reflection = new ReflectionProperty('foo', 'bar'); var_Dump($reflection->hasDefaultValue()); var_Dump($reflection->getDefaultValue()); var_Dump(hasImplicitNullDefaultValue($reflection)); echo PHP_EOL . '$baz' . PHP_EOL; echo 'Null is explicitly allowed as type, so the implicit null is fine' . PHP_EOL; $reflection = new ReflectionProperty('foo', 'baz'); var_Dump($reflection->hasDefaultValue()); var_Dump($reflection->getDefaultValue()); var_Dump(hasImplicitNullDefaultValue($reflection));
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 - 8.5.1
$foo Implicit null set. As we do not know otherwise null is fine bool(true) NULL bool(true) $bar null can not be an implicit value as this should be an int bool(true) NULL bool(false) $baz Null is explicitly allowed as type, so the implicit null is fine bool(true) NULL bool(true)
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:
88.88 ms | 407 KiB | 5 Q