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));

preferences:
37.26 ms | 407 KiB | 5 Q