php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70846 Defect?Get syntax error when declaring a variable of anonymous method in class
Submitted: 2015-11-03 17:00 UTC Modified: 2015-11-05 07:04 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: vainman at foxmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: all/allmost
Private report: No CVE-ID: None
 [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; }
    );
}
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-03 20:45 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-11-03 20:45 UTC] 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-04 15:33 UTC] 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-05 07:04 UTC] vainman at foxmail dot com
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...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 18:01:31 2024 UTC