Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 10:43
    kocimiros starred PHP-DI/Invoker
  • 08:53
    mnapoli commented #699
  • 08:51

    mnapoli on master

    Enable PHP 7.4 on travis Upgrade to PHP ^7.2 and PHPUnit… Fix test namespace and 2 more (compare)

  • 08:51
    mnapoli closed #699
  • 08:51
    mnapoli commented #699
  • Dec 13 14:59
    jdreesen synchronize #699
  • Dec 13 14:56
    jdreesen commented #699
  • Dec 13 14:52
    jdreesen commented #699
  • Dec 13 14:38
    jdreesen synchronize #699
  • Dec 13 10:50
  • Dec 13 10:49
    s0lus starred PHP-DI/PHP-DI
  • Dec 13 09:09
    falkenhawk commented #699
  • Dec 12 18:56
    jdreesen commented #699
  • Dec 12 16:31
    mnapoli commented #699
  • Dec 12 16:31
    mnapoli commented #699
  • Dec 12 16:26
    falkenhawk commented #699
  • Dec 12 16:25
    falkenhawk commented #699
  • Dec 12 11:24
    falkenhawk commented #699
  • Dec 12 11:24
    falkenhawk commented #699
  • Dec 12 11:21
    falkenhawk commented #699
ashick1234
@ashick1234
DSF
SF
H
JU
K
K
JH
JH
H
N
NH
H
NH
HNH
GH
H
GH
GH
GHGH
GH
GJ
GHGGH
TYSEZSDEGHHHHVRFYNHR
DFRDFGN
TRGNM
GHNNHEGHGHRG
seugenev
@seugenev

as i understood definitions are instructions on how classes should be instantiated once requested
im trying to create a definition for the class to call some method which will receive container itself

twig works good by my Model class doesnt. what is wrong ?


use Twig\Environment;
use Core\Model;
use Psr\Container\ContainerInterface;

return [
    Environment::class => function () {
        $loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/../App/Views');
        return new \Twig\Environment($loader);
    },
    Model::class => function (ContainerInterface $container){
        $model = new Model();
        $model->setContainer($container)->getConnection();
        return $model;
    }
];
seugenev
@seugenev
Model class code listed below.
when i use constructor way (commented) without any definitions everything works good, but i want to make possible to use it outside container so i want free constructor from the container dependency. later method will be renamed to smth like getContainerConnection and getConnection will be without container dependency then it all will be refactored and so on ... but for now i need to release constructor and create some definition to instruct DI container on how Model class should be created like: should be called setContainer method and fed with the Container itself after that should be called getConnection and returned instance of Model
    public function __construct()
    {
    }

    public function setContainer(ContainerInterface $container)
    {
        $this->container = $container;

        return $this;
    }

    public function getConnection()
    {
        $dbType = ucfirst($this->container->get('db.type'));

        $class = self::DB_NAMESPACE . $dbType;

        try {
            $this->db = $this->container->get($class)->getConnection();
        } catch (\Exception $e) {
            echo $e->getMessage();
        }

        return $this->db;
    }
    /*
        public function __construct(ContainerInterface $container)
        {
            $dbType = ucfirst($container->get('db.type'));
            $class = self::DB_NAMESPACE . $dbType;

            try {
                $this->db = $container->get($class)->getConnection();
            } catch (\Exception $e) {
                echo $e->getMessage();
            }
        }
*/
imbertjules
@imbertjules
slt
Anaël Ollier
@nanawel

Hi there,
I have a question about \DI\get() and the documentation saying that it's a "alias" to any existing definition. The result I get is a bit misleading when dealing with non-singleton objects. Here's my example:

class UniqId {
    public $id;
    public function __construct() {
        $this->id = uniqid();
    }
}
class OtherClass {
    public $id;
    public function __construct(UniqId $u) {
        $this->id = $u->id;
    }
}
$containerBuilder = new DI\ContainerBuilder();
$containerBuilder->addDefinitions([
    UniqId::class       => \DI\create(),
    'UniqId_alias'      => \DI\get(UniqId::class),
    OtherClass::class   => \DI\autowire(),
    'OtherClass_alias'  => \DI\get(OtherClass::class),
    'OtherClass_alias2' => \DI\autowire(OtherClass::class)
        ->constructorParameter('u', \DI\get('UniqId_alias'))
]);
$container = $containerBuilder->build();

var_dump([
    'UniqId            ' => $container->make(UniqId::class)->id,
    'UniqId (again)    ' => $container->make(UniqId::class)->id,
    'UniqId_alias      ' => $container->make('UniqId_alias')->id,
    'OtherClass        ' => $container->make(OtherClass::class)->id,
    'OtherClass_alias  ' => $container->make('OtherClass_alias')->id,
    'OtherClass_alias2 ' => $container->make('OtherClass_alias2')->id,
]);

The result is as follows:

array (size=6)
  'UniqId            ' => string '5d0e6194728ec' (length=13)
  'UniqId (again)    ' => string '5d0e619472911' (length=13)
  'UniqId_alias      ' => string '5d0e61947294e' (length=13)
  'OtherClass        ' => string '5d0e61947294e' (length=13)
  'OtherClass_alias  ' => string '5d0e61947294e' (length=13)
  'OtherClass_alias2 ' => string '5d0e61947294e' (length=13)

My idea was that as UniqId is defined as a non-singleton because of \DI\create(), so were its references when used as dependencies of other classes (here OtherClass ot its aliases). But actually it's not. The only solution I've found is to "force" it when defining the dependent class:

...
'OtherClass_alias3' => \DI\autowire(OtherClass::class)
        ->constructorParameter('u', \DI\create(UniqId::class))
...
but this way, we loose the power of creating a particular definition only once.

To be more precise about what results I'm confused about, it's:

  'OtherClass        ' => string '5d0e6316dbd5a' (length=13)
  'OtherClass_alias  ' => string '5d0e6316dbd5a' (length=13)
  'OtherClass_alias2 ' => string '5d0e6316dbd5a' (length=13)

where clearly the instance of UniqId which is injected in OtherClass is a singleton, when I would like to have a new instance each time a new OtherClass is created.

Richard Quadling
@rquadling
Is there a way to force a new instance even if the developer calls get() rather than make()?
nperez-messagebird
@nperez-messagebird
Howdy folks, does anyone have a rough comparison between php-di and other DI frameworks?
Simon Müller
@boscho87
ist the DI working with symfony 4?
LVdesign
@lvdesign
hello, iI use php DI 6.0 and I have this error :
Uncaught DI\NotFoundException: No entry or class found
Where I could go to resolve this ?
LVdesign
@lvdesign
I use php/DI v6.0.9
Some problem in there ?
Amir Roushenas
@roushenas
Is there any tutorial to build a sample application from scratch is available? The existing ones are too complicated for a new bee, and they just talked very generally...
Bram Van der Sype
@Brammm
@mnapoli Any suggestions on how to resolve circular dependency issues? I have a request handler that needs a command bus to handle a command, that command triggers an event that’s emitted on an event bus, which has an event listener that also requires the commandbus. I thought this wouldn’t be an issue, but apparently it is.
Anaël Ollier
@nanawel
@Brammm What you might want to try is to inject one of the dependencies via setter and not via constructor. This way you should be able to construct an (still incomplete) object, and then add the missing dependency, without falling into a CD issue. See http://php-di.org/doc/php-definitions.html#objects
ediv
@ed-iv
Hello, does anyone have a link with some documentation on how to use PHP definitions to instruct the container to create a new instance when resolving something out of the container (like make())? My attempts keep resulting in objects with the same spl_object_hash().
Anaël Ollier
@nanawel
@ed-iv In my understanding you can't. If you want to create a new instance, you must use make(), otherwise you'll get the same object as you observed.
ediv
@ed-iv
Hey, @nanawel, thanks for the reply. What I tried to do was to register a factory class in the container which is responsible for creating new instances for me. Then I inject that factory class into my controllers via the constructor. However, even in this case it seems like when I use the factory within the constructor to generate a new instance I'm still just getting the same instance. Does that line up with your understanding too?
ediv
@ed-iv
I take it back, after some additional testing this morning I see that the above strategy of registering a factory class in the container and then injecting it to fetch new instances is working as I had expected. Must have been doing something else wrong before.
Anaël Ollier
@nanawel
Yes, this is the right way to do it. Glad you figured it out :)