How to use Aura Dependency Injector (Aura.Di 3.x)?

82 Views Asked by At

I'm just trying a very simple test

<?php

require 'vendor/autoload.php';

class Blog
{
    public function post ()
    {
        return 'ok';
    }
}

$builder = new \Aura\Di\ContainerBuilder();
$blog = $builder->newInstance('Blog');
echo $blog->post();

This results to:

Fatal error: Uncaught Error: Call to undefined method Aura\Di\Container::post()

Am I missing something?

1

There are 1 best solutions below

4
Hari K T On BEST ANSWER

Yes , you are missing to read the docs. You have created builder. Next you need to get the di via new instance. This is what you assigned to blog variable.

Please consider reading getting started http://auraphp.com/packages/3.x/Di/getting-started.html#1-1-1-2

// autoload and rest of code 
$builder = new \Aura\Di\ContainerBuilder();
$di = $builder->newInstance();

Now you create instance of object

$blog = $di->newInstance('Blog');
echo $blog->post();

Please read the docs.