Php 似乎无法识别 $this 变量
Php doesn't seem to recognize the $this variable
我写了一些代码,它有一些奇怪的行为。 它会为我声明的所有私有和受保护变量抛出致命错误,即使我在它们前面使用 $this
。 $this
变量的范围似乎无法识别。
我使用 php 7.1.0 版和 apache 2.4.23 版(我安装了 mpm worker),Netbeans,Ubuntu 16.04。我还使用 pThreads (https://pecl.php.net/package/pthreads)。我在互联网上搜索并没有发现任何与此问题类似的问题。
我的 class 扩展的池 class 是 pThreads 的 class。
例如
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 Fatal error: Uncaught Error: Cannot access private property interfacePool::$timeoutStart in /usr//local/apache2/htdocs/01_Web/controllers/interface.controller.php:21
堆栈跟踪:
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)
.
class interfacePool
在 interface.controller.php
文件中(我不是想从其他地方访问这些变量)。
这些错误贯穿整个项目;我到处都有保护或私有变量。
这只是 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
作用域在内部相当于不在任何 class 上下文中(即没有私有或受保护的访问),请在此处解释您的行为。
更新:已在 https://github.com/krakjoe/pthreads/commit/ec1b2fdd6e562db7224662ed79125d8f6dde9f44
中修复
我写了一些代码,它有一些奇怪的行为。 它会为我声明的所有私有和受保护变量抛出致命错误,即使我在它们前面使用 $this
。 $this
变量的范围似乎无法识别。
我使用 php 7.1.0 版和 apache 2.4.23 版(我安装了 mpm worker),Netbeans,Ubuntu 16.04。我还使用 pThreads (https://pecl.php.net/package/pthreads)。我在互联网上搜索并没有发现任何与此问题类似的问题。
我的 class 扩展的池 class 是 pThreads 的 class。 例如
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 Fatal error: Uncaught Error: Cannot access private property interfacePool::$timeoutStart in /usr//local/apache2/htdocs/01_Web/controllers/interface.controller.php:21
堆栈跟踪:
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)
.
class interfacePool
在 interface.controller.php
文件中(我不是想从其他地方访问这些变量)。
这些错误贯穿整个项目;我到处都有保护或私有变量。
这只是 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
作用域在内部相当于不在任何 class 上下文中(即没有私有或受保护的访问),请在此处解释您的行为。
更新:已在 https://github.com/krakjoe/pthreads/commit/ec1b2fdd6e562db7224662ed79125d8f6dde9f44
中修复