I'm not sure, but you can try something like
<?php
include 'included.php';
$instances = array();
while(true)
{
foreach ($instances as $key => $instance) {
if ($result = stream_get_contents($instance)) {
pclose($instance);
unset($instances[$key]);
$o = unserialize($result);
// do something with the object
}
}
$instances[] = popen('php /path/to/include.php', 'r');
}
?>
<!-- in include.php -->
<?php
include 'included.php';
echo serialize(new tester());
?>
Or you can move all logic to "include.php" so the main script doesn't care about result at all, you will need to return just anything so that the main script knew that it may close the process handle.
Here we launch a script in separate process with popen('php /path/to/include.php', 'r');
and get a readable pipe to it (so we can read what that process outputs). When the object is successfully created in that separate process we serialize and output the result. Back in the main script with every iteration we check on already opened instances and if some of them returns something - we treat those contents as serialized object, unserialize it and then do with it whatever we want; then after doing what we wanted we close that process and remove it from opened instances list. Actually if that while(true)
in your code doesn't really run forever but rather until certain condition is met - it would be a good idea to move the processing part to separate cycle, after the initialization... LIke
while($condition)
{
$instances[] = popen('php /path/to/include.php', 'r');
}
while ( !empty($instances) ) {
foreach ($instances as $key => $instance) {
if ($result = stream_get_contents($instance)) {
pclose($instance);
unset($instances[$key]);
$o = unserialize($result);
// do something with the object
}
}
}