how to create threads in php

Solutions on MaxInterview for how to create threads in php by the best coders in the world

showing results for - "how to create threads in php"
Alan
30 Nov 2018
1class Task extends Threaded
2{
3    public $response;
4
5    public function someWork()
6    {
7        $content = file_get_contents('http://google.com');
8        preg_match('~<title>(.+)</title>~', $content, $matches);
9        $this->response = $matches[1];
10    }
11}
12
13$task = new Task;
14
15$thread = new class($task) extends Thread {
16    private $task;
17
18    public function __construct(Threaded $task)
19    {
20        $this->task = $task;
21    }
22
23    public function run()
24    {
25        $this->task->someWork();
26    }
27};
28
29$thread->start() && $thread->join();
30
31var_dump($task->response);
32
Marwan
22 May 2019
1$task = new class extends Thread {
2    private $response;
3
4    public function run()
5    {
6        $content = file_get_contents("http://google.com");
7        preg_match("~<title>(.+)</title>~", $content, $matches);
8        $this->response = $matches[1];
9    }
10};
11
12$task->start() && $task->join();
13
14var_dump($task->response); // string(6) "Google"
15