thephpleague / container

Small but powerful dependency injection container

Home Page:http://container.thephpleague.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is the documentation wrong?

itosho opened this issue Β· comments

Hello πŸ˜„

I tried Basic Usage.
But I can't do it at my pleasure 😒

<?php declare(strict_types=1);

namespace Acme;

class Foo
{
    /**
     * @var \Acme\Bar
     */
    public $bar;

    /**
     * Construct.
     *
     * @param \Acme\Bar $bar
     */
    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }
}

class Bar {}
<?php declare(strict_types=1);

require 'vendor/autoload.php';

$container = new League\Container\Container;

$container->add(Acme\Foo::class)->addArgument(Acme\Bar::class);
$container->add(Acme\Bar::class);

$foo = $container->get(Acme\Foo::class);

var_dump($foo instanceof Acme\Foo); // It's false!!! 

I think $foo is Foo Instance, but actually $foo is String, "Acme\Foo"
(I use 3.3.0.)

Could you help me? πŸ™‡

@itosho I created this repo from your code and it works fine https://github.com/philipobenito/basic-container-usage

I assume it is related to autoloading, I suggest using this as a base.

Well, I think I got it.

Is this right way?
https://github.com/itosho/playground-php/blob/master/container/basic.php

At first, I was dividing the file.

@philipobenito Sorry, I missed your comment πŸ™‡
And thank you very much! I now have a deeper understanding of the topic!

@itosho no problem, do you have it working now? My assumption is that your autoloading wasn't working

@philipobenito Now, I have it working right! As you said, I misunderstood the setting of autoloading.
I'm really thankful to you!