php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #72662 Anonymous function example 5 relies on PHP 7 syntax
Submitted: 2016-07-23 23:35 UTC Modified: 2016-07-24 11:23 UTC
From: playit2thelimit at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.6.24 OS: Windows 7
Private report: No CVE-ID: None
 [2016-07-23 23:35 UTC] playit2thelimit at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/functions.anonymous
---

Trying to understand example #5, but it does not compile.

Thanks!
  - Chris

Test script:
---------------
<?php

class Test
{
    public function testing()
    {
        (function() {
            var_dump($this);
        })();
    }
}

(new Test)->testing(); // as of PHP 5.4
    
?>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-24 05:49 UTC] requinix@php.net
-Summary: Parse error: syntax error, unexpected '(' in C:\ .... +Summary: Anonymous function example 5 relies on PHP 7 syntax -Package: Documentation problem +Package: Class/Object related
 [2016-07-24 05:49 UTC] requinix@php.net
http://php.net/manual/en/functions.anonymous.php#example-203

Yeah, that (function{})() syntax requires PHP 7.

I'm not sure it's the best example, either. The docs are trying to show that $this is available inside the function because the function was defined within the class, but in the example it's both defined *and called from* within the class.
I think code like <https://3v4l.org/qVEF1> demonstrates the concept better.
 [2016-07-24 11:23 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=339708
Log: Fix #72662: Anonymous function example 5 relies on PHP 7 syntax
 [2016-07-24 11:23 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2016-07-24 11:23 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2020-02-07 06:07 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=7308de91f090dca5ea5d54800f3c3fb4801e99fc
Log: Fix #72662: Anonymous function example 5 relies on PHP 7 syntax
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC