Edit report at https://bugs.php.net/bug.php?id=70846&edit=1
ID: 70846
User updated by: vainman at foxmail dot com
Reported by: vainman at foxmail dot com
Summary: Defect?Get syntax error when declaring a variable of
anonymous method in class
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: all/allmost
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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...
Previous Comments:
------------------------------------------------------------------------
[2015-11-04 15:33:45] vainman at foxmail dot com
Thank you for your replying.
I didn't mean it is a bug. I should have changed "Bug Type" to "Feature/Change
Requeset", and I am sorry that i haven't.
I just feel strange why i can't initialize a class member or static member with closures, but
initializing normal(global or local) variables is OK. Or why a closures isn't a constant
values. I used to think it is designed because of some special reason, and I can't find the
reason.
I am sorry about disturbing you about "such a little thing".
Just ignore this page.
------------------------------------------------------------------------
[2015-11-03 20:45:02] requinix@php.net
You can only initialize variables to constant values. Like 1 or "foo" or array(). You can
use some operators so long as the result is still constant, like 1+2 or
"foo"."bar".
You cannot use closures.
More: https://wiki.php.net/rfc/const_scalar_exprs
------------------------------------------------------------------------
[2015-11-03 17:00:56] 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; }
);
}
?>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70846&edit=1