Php似乎无法识别$ this变量

时间:2016-12-12 21:58:12

标签: php apache ubuntu this private

我写了一些代码,它有一些奇怪的行为。 它会为我声明的所有私有和受保护变量抛出致命错误,即使我在它们前面使用$this 。似乎无法识别$this变量的范围。

我使用php版本7.1.0和apache版本2.4.23(我安装了mpm worker),Netbeans,Ubuntu 16.04。我也使用pThreads(https://pecl.php.net/package/pthreads)。我在互联网上搜索并没有发现类似这个问题。

我的类扩展的Pool类是一类pThreads。 e.g。

class interfacePool extends Pool {

public $data = array();
private $workerCount;
private $timeoutStart; 
private $timeout = 50; 

public function process() {

    $this->timeoutStart = microtime(true);

    $this->workerCount = count($this->workers);

    while ($this->workerCount > 0 && $this->timeoutStart + (float)$this->timeout > microtime(true)) {

        $this->collect(function ($task) {

            if ($task->isCompleted()) {

                $this->data = array_merge($this->data, json_decode($task->data, true));
                $this->workerCount--;

            }
            return $task->isCompleted();

        });

    }

    $this->shutdown(); 

    return $this->data;

}

}

我得到的错误如下:

  

PHP致命错误:未捕获错误:无法访问/usr//local/apache2/htdocs/01_Web/controllers/interface.controller.php:21中的私有属性interfacePool :: $ timeoutStart

堆栈追踪:

0 /usr/local/apache2/htdocs/01_Web/controllers/interface.controller.php(110): interfacePool->process()

1 /usr/local/apache2/htdocs/01_Web/libs/core.class.php(221): interfaceCtrl->getTariffs()

2 /usr/local/apache2/htdocs/01_Web/index.php(35): core->run()

3 {main}
  thrown in /usr/local/apache2/htdocs/01_Web/controllers/interface.controller.php on line 21

发生错误的行是$this->timeoutStart = microtime(true)

interfacePool位于interface.controller.php文件中(我不是试图从其他地方访问这些变量)。 这些错误贯穿整个项目;我到处都有保护或隐私变量。

1 个答案:

答案 0 :(得分:4)

这只是pthreads中的一个错误。

https://github.com/krakjoe/pthreads/commit/c521adc7b645b9a60f8c3e9b6f1331c7dc6b428b错误地使用EG(fake_scope),结果是构造函数调用的NULL范围而不是zend_get_executed_scope。 (此行fcc.calling_scope = scope;应为fcc.calling_scope = zend_get_executed_scope();。)

NULL范围在内部等同于不在任何类上下文中(即没有私有或受保护的访问),在此解释您的行为。

更新:已在https://github.com/krakjoe/pthreads/commit/ec1b2fdd6e562db7224662ed79125d8f6dde9f44

中修复
相关问题