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

From: Date: Tue, 03 Nov 2015 17:01:01 +0000
Subject: Bug #70846 [NEW]: Defect?Get syntax error when declaring a variable of anonymous method in class
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-196999@lists.php.net to get a copy of this message
From:             vainman at foxmail dot com
Operating system: all/allmost
PHP version:      Irrelevant
Package:          *General Issues
Bug Type:         Bug
Bug description:Defect?Get syntax error when declaring a variable of anonymous method in class

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 bug report at https://bugs.php.net/bug.php?id=70846&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=70846&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=70846&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=70846&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=70846&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=70846&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=70846&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=70846&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=70846&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=70846&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=70846&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=70846&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=70846&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=70846&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70846&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=70846&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=70846&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=70846&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70846&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=70846&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=70846&r=mysqlcfg



Thread (4 messages)

« previous php.bugs (#196999) next »