3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CI_Controller { public function __construct() { } public function post($wl) { $data = []; foreach (array_keys($wl) as $f) { $data[$f] = $_POST[$f] ?? null; } return $data; } } class Search extends CI_Controller { public object $SearchModel; public function __construct() { parent::__construct(); $this->SearchModel = new Search_model(); $whitelist = get_object_vars($this->SearchModel); // var_export($whitelist); foreach ($this->post($whitelist) as $prop => $val) { $this->SearchModel->$prop = $val; } } public function test() { $this->SearchModel->searchByCategory(); } } class CI_Model { public function __construct() { } } class Search_model extends CI_Model { public ?int $productId = null; public ?string $productName = null; public ?string $category = null; public ?string $datetime = null; public ?int $userId = null; public function __construct() { parent::__construct(); //$this->load->database(); } public function searchByCategory() { var_dump(get_object_vars($this)); /* return $this->db->get_where('products', ['category' => $this->category])->result(); */ } } $_POST = ['productId' => '8', 'category' => 'foo']; (new Search())->test();
Output for 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
array(5) { ["productId"]=> int(8) ["productName"]=> NULL ["category"]=> string(3) "foo" ["datetime"]=> NULL ["userId"]=> NULL }

preferences:
46.64 ms | 724 KiB | 4 Q