Origin/Explanation of Class::getInstance?

15,833

Solution 1

It's quite simple. Looks, a lot like Zend Framework thought?
Anyway, this implements the Singleton design pattern. It's forbidding the programmer to create more than one instance of the controller. So, is you access your controller from anywhere in your code, instead of creating a new FrontController or something like that you just ask to be given the One instance that as been previously created.

If no instance exist, it's creating one else it only return you the front controller wich as been previously created.


Hope this helps you!

Solution 2

getInstance() comes from the Singleton pattern. You basically use this when you want to instantiate no more than one instance of a class.

Share:
15,833
meder omuraliev
Author by

meder omuraliev

I began learning front end web development around 04-05, started my first program around 06 with PHP. Currently, I am a Web technologist specializing in full stack development and linux administration specializing with the LAMP stack ( HTML5, CSS3, PHP, Apache, MySQL). I also like to dabble in node.js, meteorjs, Python, django and in general like to mess with new technology/stacks. LinkedIn | [email protected]

Updated on June 05, 2022

Comments

  • meder omuraliev
    meder omuraliev almost 2 years

    I'm fairly new to classical inheritance as I mainly deal with ECMAScript and Python, though I do a fair bit of ( shudder ) PHP. I know it's heavily influenced by Java and other classical inheritance based languages.

    Question:

    I was taking a peek at a few classes in a framework and noticed that the 'new' keyword wasn't called ( directly at least ) to create an instance, but the public getInstance method was used to create the initial object.

    Can someone explain the strategy behind 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;
        }
    }