Bug #70846 [Opn->Nab]: Defect?Get syntax error when declaring a variable of anonymous method in class

From: Date: Tue, 03 Nov 2015 20:45:04 +0000
Subject: Bug #70846 [Opn->Nab]: Defect?Get syntax error when declaring a variable of anonymous method in class
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197002@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70846&edit=1

 ID:                 70846
 Updated by:         requinix@php.net
 Reported by:        vainman at foxmail dot com
 Summary:            Defect?Get syntax error when declaring a variable of
                     anonymous method in class
-Status:             Open
+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:

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


Previous Comments:
------------------------------------------------------------------------
[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


Thread (4 messages)

« previous php.bugs (#197002) next »