Arms Tired From Passing Container Objects!

I’m starting to really enjoy the organization that the Slim framework has guided my code into. As my application grows though I am finding that I’m already passing in the same 'ol group of container objects repeatedly.

I have 2 database objects ( pdo & ORM ) and two logging routes ( text logging & database logging)

Passing all this stuff repeatedly from Route -> ModelController -> Model is getting painful because it’s happening over and over again and these are the same objects needed just about everywhere.

Maybe I’m lazy, but I hate repeating code. I came up with this:

I created a master class called Main.php which all my models and modelControllers inherit from.
This Main.php class would look like this: (abbreviated)

	namespace App\Controllers;
	class Main{
		protected static $log;
		public function __construct($log){
			self::$log = $log;

Then at the bottom of dependencies.php I just instantiate the Main.php class and set all the vars:

$main = new App\controllers\Main($container['logger']);

(I figured, all the other classes now depend on this Main.php class so it belongs here )-maybe it doesn’t, let me know?

Now each class that extends main is ready to use all of these objects: (only one in my abbreviated example)

self::$log->warn('now logging thanks to parent class',[]);

Is this a bad practice?
Does this go against some kind of design principle?

I’m sure there is a small memory overhead but I’ve always felt cpu cycles are way cheaper than developer hours these days.


*** UPDATE …

I’ve been studying up on Rob’s demo: and it looks like all of the controller’s are placed into the container right away in dependencies.php

Everything is still passed in repeatedly but I guess that’s way to do things with Slim and the dependency injection way of doing things.

You could also use PHP-DI, sure it’s a bit heavier but does exactly what you’re looking for:

There’s also a Slim bridge available:

1 Like

So I guess that’s an alternative to “pimple” which seems to come with Slim by default? I like PHP-DI’s explanations on their website by the way. They do a good job educating readers unlike most of the git hub crap I stumble upon.


Yes exactly, it replaces Pimple as a container.


I use a BaseController, which most of my other controllers extend. So in the Slim-Bookshelf example, I’d still define each controller in the container (if necessary). I have a BaseController, something like this:

class BaseController {
    protected $view;
    protected $router;
    protected $flash;

    public function __construct (View $view, Router $router, Flash $flash) {
        $this->view = $view;
        $this->router = $router;
        $this->flash = $flash;

Then many of my other controllers extend the BaseController like this.

class UsersController extends BaseController
    // class methods here

So the only duplication is in the container, but this is one area where I’m okay with duplication. Personally, I like being more explicit and i’m okay with duplication in the container and in my route definition. Just makes it easier for me to read it and see what is going on.

I don’t currently use a BaseModel as I don’t have much duplication there, at least when it comes to the constructor. Where I do have duplication within models I take care of that through Traits.

But, since I too use PHP-DI instead of Pimple most of my controllers don’t appear in my container at all and I leverage autowiring.

I think I get what you’re saying here, but I’ll have to continue on my Slim journey and try implementing this design to fully wrap my head around this. I’ve been going through the Laracasts Videos you suggested btw. (doing my homework) :slight_smile:
thanks again.

1 Like