|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-11-03 17:00 UTC] vainman at foxmail dot com
Description:
------------
Declaring a global or local variable of a anonymous method like below is OK:
----------begin----------------
$func = function($a) {
return $a;
};
----------end------------------
But in class(as public, protected, private, static, or member variable .etc) it get syntax error, like the code below
----------begin-----------------
class A{
public $func = function($a){
public $func = function($a){
return $a;
}
}
}
----------end------------------
the error info says "PHP Parse error: syntax error, unexpected T_FUNCTION, expecting ')'".
This seems make no sense to write a anonymous method, because we can just write a member method.
Actually, what I want to do is, I want to do something like the code beblow in some cases, it get the same error:
----------begin----------------
class B{
private static $a = array(
function($a) { return 1; },
function($b) { return 2; }
);
}
----------end-------------------
I guess, the PHP interpreter regard the key word "function" within class but not in other fuction as the class's member method. But in above case it is a anonymous method.
Is this a defect of PHP interpreter or it is designed?
If this is designed, can you explain the reason?
Will you fix/change it?
Test script:
---------------
<?php
$func = function($a) {
return $a;
};
class A{
public $func = function($a){
public $func = function($a){
return $a;
}
}
}
class B{
private static $a = array(
function($a) { return 1; },
function($b) { return 2; }
);
}
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 04:00:02 2025 UTC |
Today I get in some other problems about PHP's syntax paser, especially about Closure, expression and brackets. see the code below for details. Almost commentted statements get syntax errors, but i think they shouldn't. <?php class A{ public static $a; public static function initStatic(){ echo "initStatic.\n"; A::$a = function() { return 'hello'; }; } public $func; public function __construct(){ $this -> func = function() { return 'Closure func called.'; }; } } A::initStatic(); var_dump(A::$a); //OK, it's a Closuer object, but how can i call it ??????? // echo A::$a(); //syntax error: Function name must be a string // echo (A::$a)(); //syntax error: unexpected '(', expecting ',' or ';' $b = A::$a; var_dump($b); //Closure object echo $b(); //successfully execute var_dump(($b)); //Closure object // echo ($b)(); //syntax error: unexpected '(', expecting ',' or ';' ($b) is a Closure, but... $a = new A(); var_dump($a -> func); //OK, it's a Closuer object, but how can i call it ??????? // echo $a -> func(); //Fatal error: Call to undefined method A::func() // var_dump($a -> $func); //Fatal error: Cannot access empty property // echo $a -> $func(); //Fatal error: Method name must be a string var_dump(($a -> func)); //Closure object // ($a -> func)(); //syntax error, unexpected '(' $b = $a -> func; echo $b(); //successfully execute ?> =======spliter================================ <?php function func(){ return 'function func called.'; } $b = "func"; var_dump($b); //string "func" echo $b(); //successfully execute var_dump(($b)); //string "func" // echo ("func")(); //syntax error: unexpected '(', expecting ',' or ';' // echo ($b)(); //syntax error: unexpected '(', expecting ',' or ';' // echo "fu"."nc"(); //syntax error: unexpected '(', expecting ',' or ';' // echo ("fu"."nc")(); //syntax error: unexpected '(', expecting ',' or ';' (doesn't the php calculate the value of the expression first???) $b = "fu"."nc"; echo $b(); //successfully execute ?> =======spliter=============================== function getLambda(){ return function($i) { return 2 * $i; }; } $a = getLambda(); echo $a(2); //successfully execute // echo getLambda()(2); //syntax error: unexpected '(', expecting ',' or ';' but why...