How to use external Classes inside of threads?
Windows 10 php-7.2.9-Win32-VC15-x64 php_parallel-1.1.4-7.4-ts-vc15-x64
<?php
class logs{
function write(){
echo "log write".PHP_EOL;
}
}
$logs = new logs();
$fnc = static function($logs){
$logs->write();
};
$runtime = new Runtime();
$thread = $runtime->run($fnc,[$logs]);
Not works :(
Take a look at the docs
Want to join to issue. Roughly speaking, this issue doesn't related to parallel, better to anonymous function (Closures), but because of parallel using them - anyway needs a proper solution.
So, for example simple code:
<?php
class logs{
function write(){
echo "Write \n";
}
}
$logs = new logs();
$runtime = new \parallel\Runtime();
$thread = $runtime->run(function(){var_dump($logs);});
Var_dump returns NULL because any external parameters weren't passed to function. So, could you please provide solution how to use external classes here ?
Bootstrap the runtime with the necessary symbols, and use them in the task function:
$runtime->run(function() use($logs) {var_dump($logs);});
Closing this, as there's no bug here and the question is answered.