PHP 似乎无法识别 $this 变量

标签 php apache ubuntu this private

我写了一些代码,它有一些奇怪的行为。 它会对我声明的所有私有(private)变量和 protected 变量抛出 fatal error ,即使我在它们前面使用 $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 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)

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

最佳答案

这只是 pthread 中的一个错误。

https://github.com/krakjoe/pthreads/commit/c521adc7b645b9a60f8c3e9b6f1331c7dc6b428b错误地使用了 EG(fake_scope),最终导致构造函数调用的作用域为 NULL,而不是 zend_get_execulated_scope。 (此行 fcc.calling_scope =scope; 应该改为 fcc.calling_scope = zend_get_execulated_scope();。)

并且 NULL 范围在内部相当于不在任何类上下文中(即没有私有(private)或 protected 访问),在这里解释您的行为。

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

关于PHP 似乎无法识别 $this 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41110371/

相关文章:

php - 'www' 文件夹和 'htdocs' 文件夹有什么区别?

php - 连接mysql超时

javascript - Node.js 应用程序在本地正常工作,但在 Droplet Ubuntu 上出现 404 错误

php - 我可以预测我的 Zend Framework 索引有多大吗? (和一些快速 Q :s)

php - 在使用 PHP 上传之前调整照片大小

javascript - 发送请求时可以urlencode null吗?

linux - Codeigniter URL 重写 .htaccess 在 CentOS 上不起作用

php - Codeigniter - 显示与当前 session 的用户 ID 相关的数据库记录

apache - 使用 `httpd.conf` 与 `.htaccess` 重定向 url 有什么区别?

php - CLI php 在命令提示符下运行 php