parallel icon indicating copy to clipboard operation
parallel copied to clipboard

How to use external Classes inside of threads?

Open MadDogMayCry0 opened this issue 2 years ago • 2 comments

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 :(

MadDogMayCry0 avatar Aug 13 '23 16:08 MadDogMayCry0

Take a look at the docs

hschimpf avatar Aug 29 '23 19:08 hschimpf

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 ?

ntfs1984 avatar Jan 21 '24 21:01 ntfs1984

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.

krakjoe avatar May 18 '24 17:05 krakjoe