|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-01-04 19:14 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 09:00:01 2025 UTC |
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.