O
O
OneTwoThreeFourFive2020-06-10 22:36:17
PHP
OneTwoThreeFourFive, 2020-06-10 22:36:17

Why use such a construct in PHP?

class Class_Name {
  private static $init;
  
  public static function instance() {
    if ( is_null( self::$init ) ) {
      self::$init = new self();
    }
    return self::$init;
  }

  private __construct() {

  }
}

Class_Name::instance();

That is, there is a check to see if there is already an instance of the class. Why this check if I create 1 instance using
new Class_Name();
In what cases can several objects be created or this check can not be done?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2020-06-10
@OneTwoThreeFourFive

it's a classic singleton .
at what realized through .

H
HemulGM, 2020-06-11
@HemulGM

It is used anywhere, if that

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question