- var_dump: documentation ( source)
- get_object_vars: documentation ( source)
<?php
trait Singleton
{
private static ?object $instance = null;
public static function GetInstance(): object {
// Reflection better here for Dependency Injection into the instance
return self::$instance ?? (self::$instance = new self());
}
public function __set(string $property, string $value) {
$this->{$property} = $value;
}
public function __get(string $property): mixed {
return $this->{$property} ?? null;
}
protected function __construct() {}
private function __clone() {}
}
class User
{
use Singleton;
protected function __construct() {
// Change this to the select inner join query
foreach(['id' => 1, 'column' => 'test', 'points' => 3] as $column => $value)
$this->$column = $value;
}
public function save(): mixed {
// Change this to an update query
return get_object_vars($this);
}
}
User::getInstance()->points = 5; // Dynamically change values
var_dump(User::getInstance()->save()); // Then update