Use late static binding in traits
This commit is contained in:
parent
3f5bcbf26b
commit
ac57fcfd32
|
@ -45,12 +45,12 @@ trait Getter
|
||||||
{
|
{
|
||||||
$method = 'magicGet' . ucfirst($property);
|
$method = 'magicGet' . ucfirst($property);
|
||||||
if (
|
if (
|
||||||
property_exists(get_called_class(), $property)
|
property_exists(static::class, $property)
|
||||||
&& method_exists(get_called_class(), $method)
|
&& method_exists(static::class, $method)
|
||||||
) {
|
) {
|
||||||
return $this->$method();
|
return $this->$method();
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidArgumentException('Invalid property or missing getter method for property: ' . get_called_class() . '->' . $property . '.');
|
throw new InvalidArgumentException('Invalid property or missing getter method for property: ' . static::class . '->' . $property . '.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,12 +46,12 @@ trait Setter
|
||||||
{
|
{
|
||||||
$method = 'magicSet' . ucfirst($property);
|
$method = 'magicSet' . ucfirst($property);
|
||||||
if (
|
if (
|
||||||
property_exists(get_called_class(), $property)
|
property_exists(static::class, $property)
|
||||||
&& method_exists(get_called_class(), $method)
|
&& method_exists(static::class, $method)
|
||||||
) {
|
) {
|
||||||
$this->$method($value);
|
$this->$method($value);
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidArgumentException('Invalid property or missing setter method for property: ' . get_called_class() . '->' . $property . '.');
|
throw new InvalidArgumentException('Invalid property or missing setter method for property: ' . static::class . '->' . $property . '.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,20 +40,22 @@ trait Singleton
|
||||||
*
|
*
|
||||||
* @param mixed ...$args Parameters for the constructor
|
* @param mixed ...$args Parameters for the constructor
|
||||||
*/
|
*/
|
||||||
public static function getInstance(mixed ...$args): self
|
final public static function getInstance(mixed ...$args): static
|
||||||
{
|
{
|
||||||
$class = static::class;
|
$class = static::class;
|
||||||
if (!isset(static::$singleton[$class])) {
|
if (!isset(static::$singleton[$class])) {
|
||||||
static::$singleton[$class] = new $class(...$args);
|
static::$singleton[$class] = new static(...$args);
|
||||||
}
|
}
|
||||||
return static::$singleton[$class];
|
return static::$singleton[$class];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a singleton class, thus the constructor is protected.
|
* This is a singleton class, thus the constructor is private.
|
||||||
* (Get an instance of this class by calling self::getInstance())
|
* (Get an instance of this class by calling self::getInstance())
|
||||||
|
*
|
||||||
|
* @param mixed ...$args Parameters for the constructor
|
||||||
*/
|
*/
|
||||||
abstract protected function __construct();
|
abstract private function __construct(mixed ...$args);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a singleton class, thus cloning is prohibited.
|
* This is a singleton class, thus cloning is prohibited.
|
||||||
|
|
Loading…
Reference in New Issue