Frage

I downloaded a very simple MVC, and I am converting it to how I like it to be.

I see that in the index.php entry point file, a register object (class code below) is instantiated and a template and router object are passed to it that will be accessible globally in the MVC architecture.

What is the difference with just using a global array instead? Isn't that simpler? Or is there an even better way of handling this?

I'm just looking for what would be a better way of passing these objects.

I have read that magic methods are not good because it's hard to debug and re-factor. But I have read similar things about global arrays.

Class Registry {
    private $vars = array();

    public function __set($index, $value) {
        $this->vars[$index] = $value;
    }

    public function __get($index) {
        return $this->vars[$index];
    }
}
War es hilfreich?

Lösung

If your question is just about "what's with this Registry class, is it better than an array?" then: this class is just a wrapper around an array, it's useless. IMO it's because someone thought that using classes instead of arrays automatically makes their code object oriented (which is wrong). Or maybe he/she just wanted to give the array a fancy name to make its purpose clearer (Registry).

You can drop that class and use an array instead if you want to, you won't loose anything.

Or if you want to do even better, you can start to learn about dependency injection and DI containers (which is an alternative pattern to the "registry" pattern == global array).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top