|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-08-12 10:59 UTC] bar105 at zepler dot net
[2011-01-01 16:05 UTC] jani@php.net
-Package: Feature/Change Request
+Package: Class/Object related
[2015-03-14 16:03 UTC] nikic@php.net
-Status: Open
+Status: Closed
-Assigned To:
+Assigned To: nikic
[2015-03-14 16:03 UTC] nikic@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 23:00:01 2025 UTC |
Description: ------------ The parser does not permit the use of a class constant with the value NULL as a default value for a type-hinted function parameter. Using class constants instead of specific values such as "NULL" promotes a cleaner API, and more easily readable code. consider the following function, which might retrieve data from a database either for a specific date or for all dates: $foo->getInfo(Date::today()); // If given null, retrieve for all dates $foo->getInfo(null); // Using a class constant is much more readable $foo->getInfo(Foo::ALL_DATES); The parser should, imho, resolve the value of class constants before throwing an error stating the default value is not NULL and therefore not permitted. Please consider correcting this behaviour. Reproduce code: --------------- class Foo { const NO_VALUE = null; public static function bar(Foo $param = self::NO_VALUE) { if ($param == self::NO_VALUE) echo "Param was NO_VALUE" else echo "Param was a valid instance of Foo"; } } Expected result: ---------------- $myFoo = new Foo(); Foo::bar(my$Foo); // "Param was a valid instance of Foo" Foo::bar(); // "Param was NO_VALUE" Actual result: -------------- Fatal error: Default value for parameters with a class type hint can only be NULL