This code initializes the curl 2 times and closes it, my goal is to start the curl once, and then make several curl_exec requests.
//config.php $c = [ 0 => class1::class, 1 => class2::class ]; //класс курла class Curl { protected $ch; function __construct() { $this->init(); } function request($method, $url, $options = []) { return 'parsed'; } function init() { $this->ch = curl_init(); var_dump('init'); } function __destruct() { curl_close($this->ch); } } //класс ответа сервера class Response { function get() { global $c; $arr = []; foreach ($c as $item) { $super = new $item(); /** * @var Superclass $super */ $arr[] = $super->request(); } return $arr; } } //класс родительский class Superclass { //общий функционал function request() { $c = new Curl(); return $c->request('GET', 'http://ya.ru'); } } //дочерние class class1 extends Superclass { //реализация метода request отличается от class2 } class class2 extends Superclass { //реализация метода request отличается от class1 } //контроллер $r = new Response(); var_dump($r->get()); The first thing that comes to mind is to move $c = new Curl() to the very top, for example, in Response, where there is a foreach loop, then you have to pass $ c as a parameter to the class1 / class2 constructor.
Maybe there is a more optimized version? Through static?
