Req #49233 [Opn->Csd]: Type hinted parameter default value cannot be a class constant defined as null
Edit report at https://bugs.php.net/bug.php?id=49233&edit=1
ID: 49233
Updated by: nikic@php.net
Reported by: bar105 at zepler dot net
Summary: Type hinted parameter default value cannot be a
class constant defined as null
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Class/Object related
Operating System: Linux
PHP Version: 5.2.10
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
This has been fixed in PHP 5.6.4.
Previous Comments:
------------------------------------------------------------------------
[2009-08-12 10:59:52] bar105 at zepler dot net
In the expected result, "my$Foo" should read "$myFoo".
------------------------------------------------------------------------
[2009-08-12 10:53:54] bar105 at zepler dot net
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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=49233&edit=1
Thread (3 messages)