0

I had a console application. My application had used PHP-DI for auto wiring of application and bootstrapping. My services are using mysqli, so I have created it using PHP Di as lazy and my services are using them through auto wiring.

//PHP-DI code
    $config = [
                    mysqli::class => create(mysqli::class)
                            ->constructor(env('DATABASE_HOST'),
                                    env('DATABASE_USER'),
                                    env('DATABASE_PASSWORD'),
                                    env('DATABASE'), 3306, null)->lazy(),
            myService::class => autowire()
            ....
            ];

    $builder = new ContainerBuilder();
    $builder->enableCompilation(STORAGE_DIRECTORY . 'tmp');
    $builder->writeProxiesToFile(true, STORAGE_DIRECTORY . 'tmp/proxies');
    $builder->addDefinitions($config);
    $this->container = $builder->build();

// myService Class
    class myService
    {
        private $dbService;

        public function __construct(mysqli $db)
        {
            $this->dbService = $db;
        }

        public function myMethod()
        {
            //any database operation in service
            $sql = "DELETE FROM my_table where column1 = 'test';"
            $this->dbService->query($sql);
            return $this->dbService->affected_rows;
        }
    }

If I remove lazy in mysqli definition it is working fine without an issue but when I'm using lazy, it is staring to show warnings.

Warning: App\Services\myService::myMethod(): Property access is not allowed yet in /var/www/html/src/Services/myService.php on line 48
Kathak Dabhi
  • 399
  • 3
  • 16
  • 1
    FYI, it is not enough to just create a mysqli instance. The [error reporting mode and the charset are also have to be set](https://phpdelusions.net/mysqli/mysqli_connect). – Your Common Sense Apr 09 '20 at 09:37
  • @YourCommonSense I had only passed the parameters that are available to generate mysqli instant in the constructor. https://www.php.net/manual/en/mysqli.construct.php Thanks for your suggested post. In my code both things are already set through – Kathak Dabhi Apr 09 '20 at 09:51
  • Which version are you using? A similar problem was fixed in 7.4.3 – Dharman Apr 09 '20 at 10:28
  • I'm using php-7.2 and php-di:6.0.11 – Kathak Dabhi Apr 09 '20 at 11:08
  • @Dharman I had tried with updating my docker image php-7.2 to php-7.4 but the issue still persists. And everything is working fine if I removed lazy injection. So I think there is an issue with PHP-DI when it is generating the proxy class. – Kathak Dabhi Apr 10 '20 at 04:16

0 Answers0