note 102706 added to functions.anonymous

[email protected] Tue, 1 Mar 2011 09:33:51 -0800
Newsgroups php.notes
Message-ID <[email protected]>
Anyone knows of a better way to access $this from an anonymous function?
I'm currently using the following approach:

class Hello
{
    public function world()
    {
        $array = array(1,2,3);
        $that = $this;
        return array_map(function($entry) use ($that) {
            return $that->foo($entry);
        });
    }

    public function foo($in) 
    {
        return $out;
    }
}

$hello = new Hello;
$hello->world();

Seems like a hack but works.
----
Server IP: 69.147.83.197
Probable Submitter: 69.193.179.210
----
Manual Page -- http://www.php.net/manual/en/functions.anonymous.php
Edit        -- https://master.php.net/note/edit/102706
Del: integrated  -- https://master.php.net/note/delete/102706/integrated
Del: useless     -- https://master.php.net/note/delete/102706/useless
Del: bad code    -- https://master.php.net/note/delete/102706/bad+code
Del: spam        -- https://master.php.net/note/delete/102706/spam
Del: non-english -- https://master.php.net/note/delete/102706/non-english
Del: in docs     -- https://master.php.net/note/delete/102706/in+docs
Del: other reasons-- https://master.php.net/note/delete/102706
Reject      -- https://master.php.net/note/reject/102706
Search      -- https://master.php.net/manage/user-notes.php