【发布时间】:2017-04-27 21:08:20
【问题描述】:
我写了一些代码,它有一些奇怪的行为。 它会为我声明的所有私有变量和受保护变量引发致命错误,即使我在它们前面使用 $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 类。 例如
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 文件中(我不想从其他地方访问这些变量)。
这些错误贯穿整个项目;在任何我有受保护或私有变量的地方。
【问题讨论】:
-
请也发布您的“泳池”课程。编辑:没关系,它来自 pthreads 扩展。
-
请提供您的跟踪中所有文件的代码。我唯一能想象的是,你有一些带有可调用或其他东西的动态调用。因此,您超出了范围。
-
池类来自pthreads,你可以在这里看到:github.com/krakjoe/pthreads/blob/master/classes/pool.h
-
pthreads 已经有 PHP 7.1 的版本了? Afaik 它还没有更新。
-
@PEMapModder 只有 PECL/last 标记的版本不是,但是 master 应该使用它。
标签: php apache ubuntu this private