我有一个控制台应用程序。我的应用程序使用 PHP-DI 来自动连接应用程序和引导。我的服务使用 mysqli,所以我使用 PHP Di 作为惰性创建它,我的服务通过自动连接使用它们。
//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;
}
}
如果我在 mysqli 定义中删除了惰性,它可以正常工作而没有问题,但是当我使用惰性时,它会盯着显示警告。
Warning: App\Services\myService::myMethod(): Property access is not allowed yet in /var/www/html/src/Services/myService.php on line 48