Edit report at http://bugs.php.net/bug.php?id=53652&edit=1
ID: 53652
Comment by: jerry at jmweb dot net
Reported by: jerry at jmweb dot net
Summary: Can not define function as an array element of a
class property
Status: Open
Type: Feature/Change Request
Package: Class/Object related
Operating System: WIN
PHP Version: 5.3SVN-2011-01-04 (snap)
Block user comment: N
Private report: N
New Comment:
Felipe, thank you for the prompt response!
I assume that the reason why it is "expected" behavior and not a "bug"
is because PHP's back-end code does not accept a function declaration as
described. However, I would like to know the rationale as to why it is
not allowed when we can, for example, do the following:
class Test{
public $funcArray = array();
}
$test = new Test();
$test->funcArray = array(
'funcName' => function($a){
return $a*$a;
}
);
echo $test->funcArray['funcName'](2); // outputs 4 as expected
Given that the above example works fine, I tend to consider the absence
of the reported feature to be a "bug".
Please do not take my comments harshly. I am simply trying to understand
PHP's behavior in this situation. In fact, I greatly appreciate
everything that the PHP developers have done for the community. The PHP
language has helped me make a career and I hope to one day be able to
contribute.
Previous Comments:
------------------------------------------------------------------------
[2011-01-04 22:31:09] [email protected]
This is currently an expected behavior.
Changed to Feature request.
------------------------------------------------------------------------
[2011-01-04 19:14:37] jerry at jmweb dot net
Description:
------------
The following error is displayed when a function is defined as an array
element of a class property.
Parse error: syntax error, unexpected T_FUNCTION in ...
This error was experienced in PHP 5.3.3 running on Apache 2.2.17 on
Windows.
Test script:
---------------
$funcArray = array(
'funcName' => function($a){return $a*$a;}
);
echo $funcArray['funcName'](2); // outputs 4 as expected
class testClass{
public function __construct(){}
public $funcArray = array(
'funcName' => function($a){return $a*$a;}
);
}
// The parse error is thrown for the above class definition at runtime
Expected result:
----------------
I expect the script to run without throwing an error and allow the
definition of a function as an array element of a class property just as
is the case for global arrays defined globally.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=53652&edit=1