Origin / Explanation of class :: getInstance?

I am new to classical inheritance since I mainly deal with ECMAScript and Python, although I do honest (shuddered) PHP. I know that it depends a lot on Java and other classic inheritance-based languages.

Question:

I looked into a few classes and noticed that the keyword “new” was not called (at least, at least) to create an instance, but the public method getInstance was used to create the original object.

Can someone explain the strategy for this? And when should I use it for my own classes?

Relevant Code:

class FrontController {
    public static $_instance;

    public static function getInstance() {
        if ( !(self::$_instance instanceof self) ) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
}

$front = FrontController::getInstance();
$front->route();
echo $front->getBody();

Full code:

class FrontController
{
    protected $_controller, $_action, $_params, $_body, $_url;

    public static $_instance;

    public static function getInstance()
    {
    if ( ! ( self::$_instance instanceof self) ) {
        self::$_instance = new self();
    }
    return self::$_instance;
    }

    private function __construct() {
    $this->uri = uri::getInstance();

    if ( $this->uri->fragment(0) ) {
        $this->_controller = $this->uri->fragment(0).'Controller';
    }
    else {
        $config = config::getInstance();
        $default = $config->config_values['application']['default_controller'].'Controller';
        $this->_controller = $default;
    }

    if ( $this->uri->fragment(1) ) {
        $this->_action = $this->_uri->fragment(1);
    } else {
        $this->_action = 'index';
    }
    }

    public function route() {
    $con = $this->getController();
    $rc = new ReflectionClass( $con );

    if ( $rc->implementsInterface( 'IController' ) ) {
        $controller = $rc->newInstance();

        if ( $rc->hasMethod( $this->getAction() ) ) {
        $method = $rc->getMethod( $this->getAction() );
        } else {
        $config = config::getInstance();
        $default = $config->config_values['application']['default_action'];
        $method = $rc->getMethod( $default );
        }
        $method->invoke( $controller );
    } else {
        throw new Exception('Interface iController must be implemented');
    }
    }

    public function getController() {
    if ( class_exists( $this->_controller ) ) {
        return $this->_controller;
    }
    else {
        $config = config::getInstance();
        $default = $config->config_values['application']['error_controller'].'Controller';
        return $default;
    }
    }

    public function getAction() {
    return $this->_action;
    }

    public function getBody() {
    return $this->_body;
    }

    public function setBody( $body ) {
    $this->_body = $body;
    }
}
+3
source share
2

. , Zend Framework?
, Singleton. . , , , FrontController - , One, .

, , -, .


, !

+12

getInstance() Singleton pattern. , .

+4

Source: https://habr.com/ru/post/1718098/


All Articles