We recently had a discussion if it was possible to build a trait Singleton
PHP Traits and we played around with it a possible Implementation but ran into issues
Quick solution we've found (thanks chat!):
If a trait and a class both define the same method, the one of class if used
So the Singleton trait only works if the class that uses it doesn't define a __construct()
init();
}
protected function init() {}
final private function __wakeup() {}
final private function __clone() {}
}
foo = 1;
echo "Hi!\n";
}
}
var_dump(A::getInstance());
new A();
Hi!
object(A)#1 (1) {
["foo"]=>
int(1)
}
Fatal error: Call to private A::__construct() from invalid context in ...
Demo