M
M
Maxim Kudryavtsev2016-01-29 07:43:25
PHP
Maxim Kudryavtsev, 2016-01-29 07:43:25

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()
  {
    //...
  }
}

I can’t understand one thing in this code: for what purpose does its author create an instance of an object in a static method, while prohibiting the creation of an object from outside (private constructor)? What's the point in creating a synonym for a constructor? Is this some kind of design pattern unknown to me (I will be grateful for the link) or is it just plain shitty code?
Environment: PHP 5.2 and CodeIgniter 2 if that matters.
UPD : Colleagues, I know what a Singleton looks like. My first thought was that this is just its castrated version, and in some incomprehensible way ... The only overloaded magic method from PHP__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

3 answer(s)
S
Sergey, 2016-01-29
@kumaxim

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 /* ... */
));

The main idea here is to make it possible to set a sequence of actions, and at the same time to forbid the user code to somehow violate this sequence. All in order to limit a person from possible mistakes.
The difference here is that we do not return an instance of the class, it is one-time. For this sequence only.
Why not do this, you ask:
Because we don't need this instance. The reboot function should not return anything, and the object that is created is a one-time one. Why so - it is necessary to look in a context. When working with processes, I can come up with a dozen cases when necessary.
In fact, one could easily make this a regular function, but the author considered that it would be more readable this way. The govnokod part here is only the class name - Reboot. Names of classes, interfaces, etc. must be nouns. We describe the type of objects. But this is the only place I can find fault with.

A
aminought, 2016-01-29
@aminought

A private constructor, a static method that creates a new instance of the class... Apparently, this is some kind of modified Singleton .

G
gro, 2016-01-29
@gro

It turns out there is a certain function that does something there.
And there is an auxiliary class that is needed only to implement this function and nowhere else.
For simplicity, this function is made as a static method of this class.
Nopremer...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question