1

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.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71

1 Answers1

0

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

rbaskam
  • 749
  • 7
  • 22
  • This is the error I'm getting now after making the changes you suggested Fatal error: Class 'Thread' not found in C:\xampp\htdocs\practice\thread1.php on line 2 – Debojit Chakraborty Aug 16 '17 at 16:01
  • You will have to recheck your installation of Thread as the above should now be correct. – rbaskam Aug 16 '17 at 16:11
  • STEP 1: I copied the pthreadVC2.dll into the C:\xampp\php directory STEP 2: Copied the php_pthreads.dll to the C:\xampp\php\ext directory STEP 3:Copied the pthreadVC2.dll to the C:\xampp\apache\bin directory and to the C:\Windows\System32 directory. STEP 4:added the line 'extension=php_pthreads.dll' to the php.ini file. Is there anything else I need to do? – Debojit Chakraborty Aug 16 '17 at 16:21
  • Have you restarted Apache from the XAMPP control panel? – rbaskam Aug 17 '17 at 07:50
  • https://stackoverflow.com/questions/42437577/php-class-thread-not-found – rbaskam Aug 17 '17 at 13:58
  • https://github.com/krakjoe/pthreads to check you have the right version – rbaskam Aug 17 '17 at 13:59
  • https://stackoverflow.com/questions/18819784/not-able-to-install-pthread-on-windows-xampp Potential Fix – rbaskam Aug 17 '17 at 14:00
  • I think I got the issue:- 'Note that only PHP 7.2+ is now supported (requiring the current master branch of pthreads). This is due to safety issues with ZTS mode on PHP 7.0 and 7.1.' And I'm using PHP 7.1.7 as this is the latest available in the apache website. – Debojit Chakraborty Aug 17 '17 at 14:48