How do I use dependency container in a clas

I have a db container and need to use it in an external class that is in the App folder. Here is my dependency container

$container['db'] = function ($container) {
    $db = $container['settings']['db'];
    $conn = db2_connect($db['database'], $db['username'], $db['password']);
    return $conn;
};

I have a class directory outside of my controller directory but inside my app directory. How do i use ‘db’ in my class directory?

One way is to pass the container to your class in its constructor.

I tried that.

<?php


namespace App\classes;


class BdTrans
{
    private $transaction_type;
    private $batch_number;
    private $transaction_date;
    private $po_type;
    private $po_number;
    private $fund;
    private $amount;
    private $vendor_number;
    private $status;
    private $inception_user_number;
    private $inception_user_name;
    private $inception_date;
    private $inception_time;
    private $chg_user;
    private $chg_date;
    private $chg_time;
    private $po_file_name;
    private $contract_number;
    private $db;

    public function __construct(
                                $transaction_type,
                                $batch_number,
                                $transaction_date,
                                $po_type,
                                $po_number,
                                $fund,
                                $amount,
                                $vendor_number,
                                $status,
                                $inception_user_number,
                                $inception_user_name,
                                $inception_date,
                                $inception_time,
                                $chg_user,
                                $chg_date,
                                $chg_time,
                                $po_file_name,
                                $contract_number,
    db $db
)
    {
        $this->transaction_type = $transaction_type;
        $this->batch_number = $batch_number;
        $this->transaction_date = $transaction_date;
        $this->po_type = $po_type;
        $this->po_number = $po_number;
        $this->fund = $fund;
        $this->amount = $amount;
        $this->vendor_number = $vendor_number;
        $this->status = $status;
        $this->inception_user_number = $inception_user_number;
        $this->inception_user_name = $inception_user_name;
        $this->inception_date = $inception_date;
        $this->inception_time = $inception_time;
        $this->chg_user = $chg_user;
        $this->chg_date = $chg_date;
        $this->chg_time = $chg_time;
        $this->po_file_name = $po_file_name;
        $this->contract_number = $contract_number;
$this->db = $db;
    }


    public function insertBdTrans() {
$conn = $this->db;
        $last_num = 0;

        //$conn = $container->get('db');
        if ($conn) {
            $sql = 'INSERT INTO bd.bdtrans
                                    (trantype,
                                     tranbatch,
                                     trandate,
                                     tranpotype,
                                     tranponumber,
                                     tranfund,
                                     tranamount,
                                     tranvndnum,
                                     transtatus,
                                     tranuser,
                                     tranusername,
                                     tranuserdate,
                                     tranusertime,
                                     transcontractnumber)
                        VALUES      (?,
                                     ?,
                                     ?,
                                     ?,
                                     ?,
                                     ?,
                                     ?,
                                     ?,
                                     ?,
                                     ?,
                                     ?,
                                     ?,
                                     ?,
                                     ?)';

            $stmt = db2_prepare($conn, $sql);

            if ($stmt) {
                $params = array($this->transaction_type, $this->batch_number, $this->transaction_date, $this->po_type, $this->po_number, $this->fund, $this->amount, $this->vendor_number, $this->status, $this->inception_user_number, $this->inception_user_name, $this->inception_date, $this->inception_time, $this->contract_number);
                $result = db2_execute($stmt, $params);
                if (!$result) {
                    error_log(db2_stmt_errormsg($stmt));
                }
            } else {
                error_log(db2_stmt_errormsg($stmt));
            }
            $last_num = db2_last_insert_id($conn); //built in db2 function that will get the id of the last inserted row
        } else {
            error_log(db2_conn_errormsg());
        }

        return $last_num;
    }
}

I get this error

Catchable fatal error: Argument 19 passed to App\classes\BdTrans::__construct() must be an instance of App\classes\db, none given, called in /www/slim/htdocs/bd/src/App/Controllers/PurchaseOrder/PoEntryController.php on line 41 and defined in /www/slim/htdocs/bd/src/App/classes/BdTrans.php on line 49

There is a good article about DI Factories for Slim controllers you may find helpful with a few different strategies shown. But perhaps I’m not understanding your question because to use 'db' in another class you would pass it to the class the same way you pass it to your controller.