Why can I not use $this as a lexical variable in PHP 5.5.4?

后端 未结 8 1630
再見小時候
再見小時候 2020-12-04 01:12
$ php --version
PHP 5.5.4 (cli) (built: Sep 19 2013 17:10:06) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
         


        
8条回答
  •  情歌与酒
    2020-12-04 02:03

    So it seems $this can be used simply if it isn't specified via the "use" keyword.

    The following echoes 'bar':

    class Foo
    {
        private $foo = 'bar';
    
        public function bar()
        {
            return function()
            {
                echo $this->foo;
            };
        }
    }
    
    $bar = (new Foo)->bar();
    
    $bar();
    

    This was reported in the php-internals mailing list and is apparently overhang from 5.3's lack of support for this functionality:

    http://marc.info/?l=php-internals&m=132592886711725

提交回复
热议问题