1
votes

I am trying to use pthreads in php using xampp. I'm using Windows. Followed the steps provided in http://php.net/manual/en/class.thread.php for installation.

This is the sample code I'm trying to compile:

class Task extends Threaded
{
    public $response;

    public function someWork()
    {
        $content = file_get_contents('http://google.com');
        preg_match('~<title>(.+)</title>~', $content, $matches);
        $this->response = $matches[1];
    }
}

$task = new Task;

$thread = new class($task) extends Thread {
    private $task;

    public function __construct(Threaded $task)
    {
        $this->task = $task;
    }

    public function run()
    {
        $this->task->someWork();
    }
};

$thread->start() && $thread->join();

var_dump($task->response);
//phpinfo();

This is the error I'm getting:

Fatal error: Class 'Threaded' not found in C:\xampp\htdocs\practice\thread1.php on line 2.

This is the phpinfo page

I tried out the solutions given for the same from-> PHP pthreads: Fatal error: Class 'Thread' not found

I tried comparing the PHP Extension Build version(VC14) and the pthread version (it was found to be same). Thread safety is also enabled. Also I tried loading the pthread_VC2.dll file into httpd.conf for Apache server.

But nothing worked. Please provide me with a solution.

1
You extended a wrong class Threaded !=Thread - Masivuye Cokile
@MasivuyeCokile Even though I change it to Thread, I'm getting the same error. - Debojit Chakraborty

1 Answers

0
votes

The line

class Task extends Threaded

should be changed to

class Task extends Thread

as shown in the first example

class workerThread extends Thread

on the PHP.net Site