1.3.4. Primitive Values

You can also define primitive values, such as nulls, booleans, integers, floats, strings, arrays, or resources:

$def->foo = 'bar';
$def->{'db.host'} = '127.0.0.1';

Because each definition is retained as a public property on Definitions, you can overwrite or modify primitive value definitions in place. For example:

$def->{'template.helpers'} = [];
$def->{'template.helpers'}[] = 'anchor';
$def->{'template.helpers'}[] = 'escape';
$def->{'template.helpers'}[] = 'input';
// etc

1.3.4.1. Lazy Resolution

Primitive value definitions may also be Lazy (described elsewhere). For example:

$def->{'db.dsn'} = $def->env('DB_DSN');
$def->{'db.user'} = $def->env('DB_USER');
$def->{'db.pass'} = $def->env('DB_PASS');

You might then Lazy-get() these values from the Container, like so:

$def->{DB::CLASS}
    ->arguments([
        'dsn' => $def->get('db.dsn'),
        'user' => $def->get('db.user'),
        'pass' => $def->get('db.pass'),
    ]);

1.3.4.2. Naming Convention

Remember that all values and objects in the Definitions share the same space for their IDs. To reduce naming conflicts among different libraries and packages, you may wish to adopt a value naming convention based on where those values will be used.

For example, given a foobar/database package with this class ...

namespace Foobar\Database;

class Connection
{
    public function __construct(
        protected string $dsn,
        protected string $username,
        protected string $password
    ) {
    }
}

... the relevant value definitions may be named for the vendor and package ...

$def->{'foobar/database:dsn'} = $def->env('DB_DSN');
$def->{'foobar/database:username'} = $def->env('DB_USER');
$def->{'foobar/database:password'} = $def->env('DB_PASS');

$def->{Foobar\Database\Connection::CLASS}
    ->arguments([
        $def->get('foobar/database:dsn'),
        $def->get('foobar/database:username'),
        $def->get('foobar/database:password'),
    ]);

... or they may be named for class constructor parameters ...

$def->{'Foobar\Database\Connection:dsn'} = $def->env('DB_DSN');
$def->{'Foobar\Database\Connection:username'} = $def->env('DB_USER');
$def->{'Foobar\Database\Connection:password'} = $def->env('DB_PASS');

$def->{Foobar\Database\Connection::CLASS}
    ->arguments([
        $def->get('Foobar\Database\Connection:dsn'),
        $def->get('Foobar\Database\Connection:username'),
        $def->get('Foobar\Database\Connection:password'),
    ]);

... or you may come up with your own convention (or no convention at all).