Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Chris Gilroy
@ChrisGilroy9_twitter
Hi all. I'm trying to grasp how to use PHP-DI, and apparently I'm not really getting it. I have the container built. I have a definition added, a DatabaseConnection::class. I can successfully grab the connection via $db = $container->get(DatabaseConnection::class), but I'm trying to understand autowiring now. If I place $test = new TestClass(); after the container is built, and inside of TestClass, I have a dependance of DatabaseConnection in it's __constructor, I thought it would inject that into the TestClass and away I go.
Apparently, that's either not how things work, or I'm doing something wrong somewhere but I can't figure out where I'm going wrong.
Birzenbae
@Birzenbae
@mandeephub Hey, my best guess is that either the mysql-version or at least the "sql_mode" setting differ. The problem is that in new versions of mysql the column selection needs to be the same as in your group by clause except if you use aggregation operates like MAX Min etc.
Mandeep Sharma
@mandeephub
@Birzenbae thanks for pointing out that issue i have informed the hosting company to reduce my sql version from 5.7 to 5.6 hope it works
Mandeep Sharma
@mandeephub
"SELECT to_startdate, to_enddate FROM tour WHERE to_name !='' AND to_startdate !='0000-00-00' AND to_deactivated !=1 GROUP BY MONTH(to_startdate), YEAR(to_startdate) ORDER BY to_startdate"
this query is not waorking in sql 5.6 and working in 5.6 how to convert 5.6 query to 5.7
Birzenbae
@Birzenbae
@mandeephub Have a look at this two links :https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
smdevdk
@smdevdk
Should the default behaviour not be to re-init classes every time they are requested in a constructor? Instead of treating every single class as a shared resources across the entire app.
Mandeep Sharma
@mandeephub
Oops php is hard
Maciej Holyszko
@falkenhawk
hi @mnapoli , I am returning to you with some of my suggestions for php-di improvements (as promised more than 1.5 year ago...) I am sorry it's taking that long from my side - I hardly worked on php projects recently (moved to js)
please take a brief look at https://github.com/ovos/PHP-DI/pulls when you have time. Feature/service locator still needs some polishing, so that its point would be somehow proven, and a test suite. I won't push with PRs if you decide it does not make sense though.
ashick1234
@ashick1234
fgyhtyutrggyhgefhuefheuhehghtrfyjutrfjutr
yyutrutryuhtruhtrhtrghtrghtyghtyhtrguitrguiujhfggherhtgghghhgghygyuyhujsdfgyasfgndcxtnfsdbtf6edetf68erwGHESDFSDF
FDSFSDFSDFSD
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?