コード例 #1
0
<?php

define('MY_CONSTANT', true);
function test()
{
}
class Foobar
{
}
class Example extends Thread
{
    public function run()
    {
        var_dump(defined('MY_CONSTANT'));
        var_dump(function_exists('test'));
        var_dump(class_exists('Foobar'));
    }
}
// true true true
$job = new Example();
$job->start();
// default argument is PTHREADS_INHERIT_ALL
$job->join();
// false false true
$job = new Example();
$job->start(PTHREADS_INHERIT_CLASSES);
$job->join();
// true true true
$job = new Example();
$job->start(PTHREADS_INHERIT_CLASSES | PTHREADS_INHERIT_CONSTANTS | PTHREADS_INHERIT_FUNCTIONS);
$job->join();
コード例 #2
0
<?php

require 'vendor/autoload.php';
class Example extends Thread
{
    public function run()
    {
        dump('foobar');
    }
}
// Faulty example
//////////////////////////////////////////////////////////////////////
$job = new Example();
$job->start();
$job->join();
// Now let's fix our example
//////////////////////////////////////////////////////////////////////
class AutoloadingWorker extends Worker
{
    public function run()
    {
        require 'vendor/autoload.php';
    }
}
// Create our worker and stack our job on it
$worker = new AutoloadingWorker();
$job = new Example();
$worker->stack($job);
$worker->start();
$worker->join();
// Or use a pool and specify our custom worker