【问题标题】:Php doesn't seem to recognize the $this variablePhp 似乎无法识别 $this 变量
【发布时间】: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


【解决方案1】:

这只是 pthread 中的一个错误。

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中修复

【讨论】:

猜你喜欢
  • 2016-09-14
  • 1970-01-01
  • 2010-11-05
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
相关资源
最近更新 更多