# Controller constructor not being called by Dependencies what gives?

**URL:** <https://discourse.slimframework.com/t/controller-constructor-not-being-called-by-dependencies-what-gives/995>\
**Category:** Questions\
**Created:** [December 13, 2016, 3:50pm UTC](https://discourse.slimframework.com/t/controller-constructor-not-being-called-by-dependencies-what-gives/995 "2016-12-13T15:50:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![codejunky](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/codejunky/32/283_2.png) [@codejunky](https://discourse.slimframework.com/u/codejunky)\
**Post date:** [December 13, 2016, 3:50pm UTC](https://discourse.slimframework.com/t/controller-constructor-not-being-called-by-dependencies-what-gives/995/1 "2016-12-13T15:50:17Z")

</div>

So I’ve setup the dependencies.php file with the following call:

//class definitions  
$container[‘MyNewController’] = function($c){

```
return new MyNewController($c->get('settings')['db'], $c->get('renderer'));

```

};

When I access the route like so:

$app-\>get(’/newcontroller/ths/home’, ‘NewController::home’);

the correct action is called without a problem. That being said, the constructor **never** gets called so I cannot access the keys from the container in the dependencies file. So two errors I see, adding the NewController class to the container / dependencies file does not init the constructor on the NewController class AND I can’t access my private vars via the $this operator, what gives??

class NewController{

private $rdb;  
private $view;

\_\_construct($rdb, $view){

```
 $this->rdb = $rdb;
 $this->view = $view;

```

}

public function home($request, $response){ //do stuff here not a problem }

}

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [December 13, 2016, 4:08pm UTC](https://discourse.slimframework.com/t/controller-constructor-not-being-called-by-dependencies-what-gives/995/2 "2016-12-13T16:08:17Z")

</div>

I assume this is pseudo code and not exactly what you have? You have an entry for `MyNewController` but your class is `NewController`. I’d think you might want to consider the visibility of your properties and your constructor as well.

```php
class NewController { // or MyNewController ?

    protected $rdb;
    protected $view;

    public function __construct($rdb, $view){
        $this->rdb = $rdb;
        $this->view = $view;
    }

    public function home($request, $response){
         //do stuff here not a problem
    }
}

```

---

<div class="post-metadata">

**Author:** ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)\
**Post date:** [December 13, 2016, 4:13pm UTC](https://discourse.slimframework.com/t/controller-constructor-not-being-called-by-dependencies-what-gives/995/3 "2016-12-13T16:13:34Z")

</div>

Use a single `:` in

```
$app->get('/newcontroller/ths/home', 'NewController:home');
```

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [December 13, 2016, 4:18pm UTC](https://discourse.slimframework.com/t/controller-constructor-not-being-called-by-dependencies-what-gives/995/4 "2016-12-13T16:18:28Z")

</div>

Ahhh… good eye. Missed that.

---

<div class="post-metadata">

**Author:** ![codejunky](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/codejunky/32/283_2.png) [@codejunky](https://discourse.slimframework.com/u/codejunky)\
**Post date:** [December 13, 2016, 5:59pm UTC](https://discourse.slimframework.com/t/controller-constructor-not-being-called-by-dependencies-what-gives/995/5 "2016-12-13T17:59:02Z")

</div>

yes guys this was pseudo…my bad …and yes I’m using :: on the app-\>get call. And yes I renamed the controller name as well. Another typo…oops! Matches in the real code though…

But any ideas though as to why the constructor doesn’t get called? The action works fine

---

<div class="post-metadata">

**Author:** ![codejunky](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/codejunky/32/283_2.png) [@codejunky](https://discourse.slimframework.com/u/codejunky)\
**Post date:** [December 13, 2016, 6:07pm UTC](https://discourse.slimframework.com/t/controller-constructor-not-being-called-by-dependencies-what-gives/995/6 "2016-12-13T18:07:31Z")

</div>

Here’s the full code so that you guys can see the real deal…

\<?php use Slim\Http\Request; use Slim\Http\Response; use Slim\Container; class MyNewController { private $rdb; private $view; function \_\_construct($rdb, $view) { $this-\>rdb = $rdb; $this-\>view = $view; } #home action public function home(Request $request, Response $response){ echo 'hello home!'; var\_dump($this-\>rdb); #fails return; } } #routes.php $app-\>get('/mynewcontroller/ths/home', 'MyNewController::home'); #dependencies.php $container['MyNewController::class'] = function($c){ return new MyNewController($c-\>get('settings')['db'], $c-\>get('renderer')); }; Again action works fine , but constructor is not called and references \*\*$this\*\* in action fails

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [December 13, 2016, 7:35pm UTC](https://discourse.slimframework.com/t/controller-constructor-not-being-called-by-dependencies-what-gives/995/7 "2016-12-13T19:35:47Z")

</div>

As the amazing @akrabat pointed out, try a single colon:

```php
$app->get('/mynewcontroller/ths/home', 'MyNewController:home');

```

And I believe in your container you just want the class name:

```php
$container['MyNewController'] = function ($c) {

```

See [Container Resolution](https://www.slimframework.com/docs/objects/router.html#container-resolution) from the docs for more examples.

---

<div class="post-metadata">

**Author:** ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)\
**Post date:** [December 14, 2016, 8:38am UTC](https://discourse.slimframework.com/t/controller-constructor-not-being-called-by-dependencies-what-gives/995/8 "2016-12-14T08:38:08Z")

</div>

You _have to use a single colon_. A double colon is interpreted by PHP as a static method call which is why $this doesn’t work.

---

<div class="post-metadata">

**Author:** ![codejunky](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/codejunky/32/283_2.png) [@codejunky](https://discourse.slimframework.com/u/codejunky)\
**Post date:** [December 14, 2016, 4:04pm UTC](https://discourse.slimframework.com/t/controller-constructor-not-being-called-by-dependencies-what-gives/995/9 "2016-12-14T16:04:34Z")

</div>

Thank you @akrabat! I’ll try that shortly and see if that fixes it for me. But definitely sounds like that’s the culprit! Good to know how the framework is interpreting the route class definitions! 🙂

thanks again,  
-cj

---

<div class="post-metadata">

**Author:** ![codejunky](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/codejunky/32/283_2.png) [@codejunky](https://discourse.slimframework.com/u/codejunky)\
**Post date:** [December 14, 2016, 4:16pm UTC](https://discourse.slimframework.com/t/controller-constructor-not-being-called-by-dependencies-what-gives/995/10 "2016-12-14T16:16:37Z")

</div>

@tflight , it was late for me yesterday and not sure why I just glossed over your comment above about the single colon…thanks though for pointing that out. Repetition is always best 🙂 and thanks to @akrabat again… i’m now off to the races!

Go slim! 🙂

-cj
