Answer the question
In order to leave comments, you need to log in
What does it look like more: a pattern or shit code?
Good day to all.
In my latest project, I often stumble upon the following construction:
class Reboot implements Process
{
private static $_folder = __DIR__;
private function __construct()
{}
public static function reboot()
{
$instance = new self;
$instance->pause();
$instance->stop();
$instance->start();
}
private function pause()
{
//...
}
}
__construct
. All the code relevant to the question is complete, the content of the pause, stop and start methods does not matter much. Besides that it used to be a Singleton, does anyone else have any ideas?
Answer the question
In order to leave comments, you need to log in
This code has nothing to do with Singleton.
This technique has many names. In fact, this is a static factory method, you can still meet the "named constructor".
class User {
private function __construct($email, $password) { /* ... */}
public static function create($email, $password) {
return new static($email, $password);
}
public static function createWithProfile($email, $password, UserProfile $profile) {
$user = static::create($email, $password);
$user->profile = $profile;
return $user;
}
}
$user1 = User::create('[email protected]', 'example');
$user2 = User::createWithProfile('[email protected]', 'example', new UserProfile(
$firstName, $lastName, $avatar /* ... */
));
A private constructor, a static method that creates a new instance of the class... Apparently, this is some kind of modified Singleton .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question