簡體   English   中英

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 )。 我在互聯網上進行搜索,未發現與此問題類似的任何內容。

我的類從其擴展的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致命錯誤:未捕獲錯誤:無法訪問私有屬性interfacePool :: $ timeoutStart在/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文件中(我不嘗試從其他位置訪問這些變量)。 這些錯誤在整個項目中都會發生。 我有保護或私有變量的任何地方。

它只是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中修復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM