PHP-DI annotations not working

I have installed php-di 4.4 in a new custom project using composer. I am running xampp localhost with php 5.6.3, but I have installed netbeans for php 5.4 in this project. I'm new to php-di, I have used annotations in my android projects, but it doesn't seem to work here. The code is simple, im testing the injection to see how it works, here is the code:

// composer autoload
require_once __DIR__ . '/vendor/autoload.php';

// injection entry point
$builder = new DI\ContainerBuilder();
$container = $builder->build();

class ClassA
{
    public function methodA()
    {
        echo 'methodA';
    }
}
class ClassB
{
    /**
     * @Inject
     * @var ClassA
     */
    public $param;

    public function methodB()
    {
        $this->param->methodA();
    }
}

$b = new ClassB();
$b->methodB();

      

this is the error i am getting: Calling member function A () at zero in D: \ Projects \ profi \ test.php on line 27

This basic implementation I don't understand why it doesn't introduce.

Thanks in advance.

+3


source to share


1 answer


PHP-DI cannot magically intercept when B is created (to inject A into B). You have to create B using PHP-DI:



$b = $container->get('ClassB');
$b->methodB();

      

+2


source







All Articles