1

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?

IMB
  • 15,163
  • 19
  • 82
  • 140

1 Answers1

1

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.

Hari K T
  • 4,174
  • 3
  • 32
  • 51
  • sorry but isn't that what my code above just did? `$blog = $builder->newInstance('Blog');` – IMB Oct 24 '18 at 05:29
  • 1
    No. Did you checked the docs ? It has very clearly stated it how to create the di instance. Updating the answer with code even though I don't love spoon feeding. – Hari K T Oct 24 '18 at 05:57
  • 2
    I seriously read the docs but didn't realize that because the example was a bit confusing due to the method names being exactly the same and there's actually an additional step to create the object. When compared to other DI's (e.g., `league/container` and `rdlowrey/auryn`) it's just usually `$container = new Container;` $container->get('Blog');` That's what caused the confusion. – IMB Oct 24 '18 at 11:36
  • 1
    The difference here is in Aura you can pass different configuration and once configuration is loaded the container is locked. And you no longer have an option to modify the container params etc. This a good thing, but some times it feels horrible. – Hari K T Oct 24 '18 at 16:51