I wrote some code and it has some weird behavior. It throws fatal errors for all the private and protected variables I have declared, even if I use them with $this
in front of them. It seems like the scope of the $this
variable is not recognized.
I use php version 7.1.0 and apache version 2.4.23 (and I installed the mpm worker), Netbeans, Ubuntu 16.04. I also use pThreads (https://pecl.php.net/package/pthreads). I searched on the internet and didn't found anything similar to this problems.
The Pool class from which my class extends is a class of pThreads. e.g.
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;
}
}
And the error I'm getting is the following:
PHP Fatal error: Uncaught Error: Cannot access private property interfacePool::$timeoutStart in /usr//local/apache2/htdocs/01_Web/controllers/interface.controller.php:21
Stack trace:
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
The line on which the error occurs is the $this->timeoutStart = microtime(true)
.
The class interfacePool
is in the interface.controller.php
file (I'm not trying to access those variables from somewhere else).
These errors occur through the whole project; everywhere I have protected or private variables.
It simply is a bug in pthreads.
https://github.com/krakjoe/pthreads/commit/c521adc7b645b9a60f8c3e9b6f1331c7dc6b428b is using EG(fake_scope)
wrongly, ending up with a NULL
scope for the constructor call instead of zend_get_executed_scope
. (This line fcc.calling_scope = scope;
should be fcc.calling_scope = zend_get_executed_scope();
instead.)
And a NULL
scope is, internally, equivalent to not being in any class context (i.e. no private nor protected access), explaining your behavior here.
Update: Fixed in https://github.com/krakjoe/pthreads/commit/ec1b2fdd6e562db7224662ed79125d8f6dde9f44