# When to use the container or a class?

**URL:** <https://discourse.slimframework.com/t/when-to-use-the-container-or-a-class/3700>\
**Category:** Questions\
**Created:** [December 3, 2019, 8:23pm UTC](https://discourse.slimframework.com/t/when-to-use-the-container-or-a-class/3700 "2019-12-03T20:23:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![margikern](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@margikern](https://discourse.slimframework.com/u/margikern)\
**Post date:** [December 3, 2019, 8:23pm UTC](https://discourse.slimframework.com/t/when-to-use-the-container-or-a-class/3700/1 "2019-12-03T20:23:06Z")

</div>

The question refers to a database connection. I’m new to the slim framework, and there was a question, should I use a container to connect to the database or create a Connection class that connects to the database and extend into a model? Both deliver the same result, but as a beginner it’s always good to ask questions

## **Example 1: Using a Class**

**Create the connection class**

 ![classConnection](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/a/a7b5ce14f437ef18f98396986846811ff48332f6.png)

**Call the connection class in a Model class**  
namespace App\models;  
use App\models\Connection;

```
class Model 
{

    protected $connect;
    protected $table;

    public function __construct()
    {
       $this->connect = Connection::connect();
    }

...

```

  

**Uses the Post class that extends the Model class that owns the database connection**

 ![useClassConnection2](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/0/0675a740b5cd7a454c0bd23f16b01f8dd3c4a08a.png)

## **Example 2 : Using container**

**Register the container DB**  
 ![containerDB](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/d/d75682e32a3ed05f2333d2061f8916282f580361.png)

**Create a Mapper class to do dependency injection**

```
abstract class Mapper
{
    protected $db;

    public function __construct(\PDO $pdo)
    {
        $this->db = $pdo;
    }
}

```

  

**Registers controller dependency container**

```
$container['Source\Controller\TicketController'] = function ($c) {
    return new TicketController($c['view'], $c['logger'], $c['db']);
};

```

  

**Use direct container in controller class**

 ![use2ContainerDB](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/3/358dd424ab3061b8b6939f3af3fe6e9105a826e9.png)

I am currently using slim framework 3.12, I know there is slim 4, but for learning I am starting at 3.

Since this is my first question on the forum, and I don’t know English, probably something in the text may not be grammatically correct because I used google translator.

If the question is large I can summarize, because I was concerned to give as much detail as possible.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [December 5, 2019, 7:40pm UTC](https://discourse.slimframework.com/t/when-to-use-the-container-or-a-class/3700/2 "2019-12-05T19:40:09Z")

</div>

> [@margikern](#):
>
> should I use a container to connect to the database or create a Connection class that connects to the database and extend into a model?

You don’t need an extra class for the PDO object. Better use the container in combination with dependency injection. Your repository should declare PDO as explicit dependency.
