# Im having trouble understanding how dependency injection actually works, or how to properly use it

**URL:** <https://discourse.slimframework.com/t/im-having-trouble-understanding-how-dependency-injection-actually-works-or-how-to-properly-use-it/482>\
**Category:** Questions\
**Created:** [July 7, 2016, 1:37pm UTC](https://discourse.slimframework.com/t/im-having-trouble-understanding-how-dependency-injection-actually-works-or-how-to-properly-use-it/482 "2016-07-07T13:37:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johan](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@johan](https://discourse.slimframework.com/u/johan)\
**Post date:** [July 7, 2016, 1:37pm UTC](https://discourse.slimframework.com/t/im-having-trouble-understanding-how-dependency-injection-actually-works-or-how-to-properly-use-it/482/1 "2016-07-07T13:37:13Z")

</div>

Hi!

So I have been trying to build myself an boilerplate with Slim3. And actually, I’m pretty surprised that it is going so smooth, until now.

I want too adapt the [MVC design pattern](http://www.phptherightway.com/pages/Design-Patterns.html), but due to that I’m having trouble to inject the models into the app. (I think)

So far I can do everything in the controller, that is injected [like this](https://github.com/ArneAnka/slim-test/blob/master/public/index.php#L104), but when trying to use for example `$this->pdo` [from a model](https://github.com/ArneAnka/slim-test/blob/master/app/Models/UserModel.php#L8), I’m receiving `Notice: Undefined property`.

When using Eloquent, this is works fine. No need for extra “injection”.

So how to I use a MVC design pattern with slim?

[my slim repo](https://github.com/ArneAnka/slim-test)

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [July 10, 2016, 1:54am UTC](https://discourse.slimframework.com/t/im-having-trouble-understanding-how-dependency-injection-actually-works-or-how-to-properly-use-it/482/2 "2016-07-10T01:54:58Z")

</div>

Unless I’m misunderstanding something here, there is no need to get $pdo when using eloquent. You can just do the following

```
class Users extends Model
{
    ...
}

```

and then use eloquent in your model code without every having to worry about using PDO. This is all abstracted away in Eloquent.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![johan](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@johan](https://discourse.slimframework.com/u/johan)\
**Post date:** [July 11, 2016, 2:15pm UTC](https://discourse.slimframework.com/t/im-having-trouble-understanding-how-dependency-injection-actually-works-or-how-to-properly-use-it/482/3 "2016-07-11T14:15:47Z")

</div>

Thanks for your reply! But I’m not using Eloquent.

Its a PDO (FluentPDO) instance I’m trying to use.

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [July 11, 2016, 7:32pm UTC](https://discourse.slimframework.com/t/im-having-trouble-understanding-how-dependency-injection-actually-works-or-how-to-properly-use-it/482/4 "2016-07-11T19:32:33Z")

</div>

You need to define pdo in the container like so…

```php
$container = $app->getContainer();
$container['pdo'] = function ($c) {
    return new \PDO(....);
};

$app->get('/test', function ($req, $res, $args) {
    $this->pdo //do something with pdo
    return $res->write("woo hoo");
});

```
