php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53652 Can not define function as an array element of a class property
Submitted: 2011-01-04 19:14 UTC Modified: 2011-01-06 03:12 UTC
From: jerry at jmweb dot net Assigned:
Status: Duplicate Package: Class/Object related
PHP Version: 5.3SVN-2011-01-04 (snap) OS: WIN
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jerry at jmweb dot net
New email:
PHP Version: OS:

 

 [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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-04 22:31 UTC] felipe@php.net
-Type: Bug +Type: Feature/Change Request
 [2011-01-04 22:31 UTC] felipe@php.net
This is currently an expected behavior.

Changed to Feature request.
 [2011-01-06 02:34 UTC] jerry at jmweb dot net
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.
 [2011-01-06 03:12 UTC] aharvey@php.net
-Status: Open +Status: Duplicate
 [2011-01-06 03:12 UTC] aharvey@php.net
Property initialisers have to be constant values: an anonymous
function doesn't count as a constant, just as any other non-constant
expression doesn't either.

This is a duplicate of bug #51813, which was closed by Johannes, so
I'm going to dupe this for now.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 01:01:33 2025 UTC