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

From: Date: Wed, 04 Nov 2015 15:33:46 +0000
Subject: Bug #70846 [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-197023@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
 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:

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.


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


Thread (4 messages)

« previous php.bugs (#197023) next »