3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Singleton { # Holds Parent Instance private static $_instance; # Holds Reference To zVal protected $_args; # Create The Instance protected function __construct() {} # Static Call To Get Instance public static function getInstance() { return self::$_instance ?? (self::$_instance = new self()); } # Bind zVal Of Passed Reference To Index protected function _bindParam(&$val) { $this->_arg = &$val; return $this; } } abstract class Model { abstract public function save(); # Set your DB up here } class MyModel extends Model { use \Singleton { _bindParam as public bindParam; } public function save() { print_r($this->_arg); } } MyModel::getInstance()->bindParam($row); foreach([['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']] as $key => $row) { $row[0] = 'Changed'; MyModel::getInstance()->save(); }
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
Deprecated: Creation of dynamic property MyModel::$_arg is deprecated in /in/ePbei on line 21 Array ( [0] => Changed [1] => B [2] => C ) Array ( [0] => Changed [1] => E [2] => F ) Array ( [0] => Changed [1] => H [2] => I )
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33
Array ( [0] => Changed [1] => B [2] => C ) Array ( [0] => Changed [1] => E [2] => F ) Array ( [0] => Changed [1] => H [2] => I )

preferences:
146.8 ms | 408 KiB | 5 Q