php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #71348 Static anonymous functions are not documented
Submitted: 2016-01-12 10:54 UTC Modified: 2016-05-22 20:06 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: korikulum at net dot hr Assigned: tpunt (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: korikulum at net dot hr
New email:
PHP Version: OS:

 

 [2016-01-12 10:54 UTC] korikulum at net dot hr
Description:
------------
---
From manual page: http://www.php.net/functions.anonymous
---

Since PHP 5.4 anonymous function can be declared statically (https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#anonymous-function-creation).

When an anonymous function is declared statically, it prevents auto-binding to the instance it is in. Also, it can not be bound using the bind, bindTo & call functions.


Test script:
---------------
class Foo
{
    public function bar()
    {
        return static function () {
            var_dump($this);
        };
    }

    public function baz()
    {
        return function () {
            var_dump($this);
        };
    }
}

$foo = new Foo();

$bar = $foo->bar();
$bar();

// Notice: Undefined variable: this
// NULL


$baz = $foo->baz();
$baz();

// object(Foo)#1 (0) {
// }


Closure::bind($bar, new StdClass());
$bar->bindTo(new StdClass());
$bar->call(new StdClass());

// Warning: Cannot bind an instance to a static closure


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-12 15:24 UTC] tpunt@php.net
-Assigned To: +Assigned To: tpunt
 [2016-05-22 19:45 UTC] tpunt@php.net
Automatic comment from SVN on behalf of tpunt
Revision: http://svn.php.net/viewvc/?view=revision&revision=339215
Log: Resolve doc bug #71348
 [2016-05-22 20:06 UTC] tpunt@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 13:01:34 2025 UTC