php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55078 Can't use anonymous function inside array declaration inside a class
Submitted: 2011-06-29 23:54 UTC Modified: 2012-11-23 09:06 UTC
Votes:4
Avg. Score:4.5 ± 0.5
Reproduced:4 of 4 (100.0%)
Same Version:3 (75.0%)
Same OS:1 (25.0%)
From: charlie at charliesomerville dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.3.6 OS:
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: charlie at charliesomerville dot com
New email:
PHP Version: OS:

 

 [2011-06-29 23:54 UTC] charlie at charliesomerville dot com
Description:
------------
You can't declare an anonymous function in an array declaration inside a class 
declaration.

Test script:
---------------
<?php

class Foo
{
	static $array = array("hi" => function() {
		phpinfo();
	});
}

Foo::$array["hi"]();

Expected result:
----------------
phpinfo being displayed

Actual result:
--------------
Parse error: syntax error, unexpected T_FUNCTION in /path/to/script.php on line 5

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-01 18:46 UTC] felipe@php.net
-Type: Bug +Type: Feature/Change Request
 [2011-07-01 18:46 UTC] felipe@php.net
This is just more one limitation, not a bug. Changing to feature request.
Thanks.
 [2012-11-23 04:38 UTC] nickmokisasian at gmail dot com
When will this feature be supported?
 [2012-11-23 09:06 UTC] laruence@php.net
-Status: Open +Status: Wont fix
 [2012-11-23 09:06 UTC] laruence@php.net
"(propeties) This declaration may include an initialization, but this 
initialization must be a constant value--that is, it must be able to be 
evaluated at compile time and must not depend on run-time information in order 
to be evaluated."
http://www.php.net/manual/pt_BR/language.oop5.properties.php

lambda is a dynamic expression like :

function lambda() use ($variable) {
}

so, we should not allow lambda in class properties initialization.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 06 19:01:28 2025 UTC