2

maybe its late or something, but this is freaking me out. Basicly im writing framework for the excercise, trying to get my oop skill rolling, but im kinda stuck. I think i maybe hit a rookie err, that i just dont know of :)

So i have a bootstrap class which initializes the whole program. I creates a controller, in which you can call an app (or model if you prefer). All controllers and apps are sub classes of an initialization class, that implements the singleton pattern. This i because i would like to use the construct function for other init stuff based on the given controller/app.

Now the problem is, when i try to init an app from within the controller, the instance var is already set to the controller object (which i thought was null). If im not clear enough, here is the code in a very simplified and raw form:

<?php

final class Bootstrap {
    public function __construct() {
        $controller = Controller::init();
        $controller->index();
    }
}

abstract class Initialization {
    private static $_instance = NULL;

    final public static function init() {
        $c = get_called_class();

        var_dump(self::$_instance);

        self::$_instance = new $c();

        return self::$_instance;
    }

    final protected function app($app) {
        $app::init();
    }
}

final class Form extends Initialization { }

final class Controller extends Initialization {
    final public function Index() {
        $this->app('form');
    }
}

$bootstrap = new Bootstrap();

?>

The output is

null
object(Controller)[2]

Im confused why is the instance already set in the form app?? I thought it inherited it's "own" static instance var??

Esben
  • 1,943
  • 1
  • 18
  • 37
  • Do youself a favor and get rid of the Singleton. You dont need it there. See [Who needs Singletons](http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323). Also, if you want to do OOP, familiarize yourself with the SOLID principles. In addition, reduce the static calls to a minimum and dont abuse inheritance with base classes. Get rid of the final keyword as well and start writing Unit-Tests to get a feeling for how much the code above will shoot you in the foot. – Gordon Jul 26 '11 at 21:25
  • I only used the singleton so i could keep my __construct(). This is not a pattern i usually implement. Do you have any recommended ressources for reading up on SOLID? i've read the description on wiki, and it looks awesome. Any particulary reason why i should leave out the final keyword? – Esben Jul 27 '11 at 15:44
  • The Wikipedia entry is a good start actually. Following the internal links and some of the linked Resources at the bottom should keep you reading for a long time. You should leave out the final keyword because it will prevent your from mocking dependencies when doing unit-tests. – Gordon Jul 27 '11 at 16:21

1 Answers1

3

You only get your 'own' static instance var if you call it with late static binding, like static::$_instance instead of self::$_instance. Also, you would want to make it protected instead of private.

self:: is determined at compile-time so to say, so will always reference Initialization::$_instance, regardless from which class extending it you call it. static:: is determined while the program runs.

Code alterations:

abstract class Initialization {
    protected static $_instance = NULL;

    final public static function init() {
        $c = get_called_class();
        static::$_instance = new $c();
        return static::$_instance;
    }
Wrikken
  • 69,272
  • 8
  • 97
  • 136