Dependency injection library for PHP8.
<?phpusePDO;useSyringe\Attribute\{Provides,Qualifier};class DbConfiguration {// bool "primary" - Sets the Provides as primary if more than one of the same type is available.// ?string "name" - Custom name (or qualifier) for the Provides. (Set "null" to use the method name)// bool "singleton" - Denotes that the Provides is a singleton #[Provides(primary:false, name:null, singleton:true)]// Default valuespublicfunctiongetMariaDBConnection():PDO {$dsn ='mysql:dbname=mariadb;host=127.0.0.1;port=3307';$user ='root';$password ='password';returnnewPDO($dsn,$user,$password); } #[Provides(primary:true)]publicfunctiongetMySQLConnection():PDO {$dsn ='mysql:dbname=mysqldb;host=127.0.0.1;port=3306';$user ='root';$password ='password';returnnewPDO($dsn,$user,$password); }/* public function getCarsRepository( // Without "Qualifier" the parameter will be bound to // "getMySQLConnection" (because it's primary for the PDO class) #[Qualifier("getMariaDBConnection")] PDO $db ): CarsRepository { return new CarsRepository($db); }*/// ...}
<?phpuseSyringe\Attribute\Inject;class TestClass { #[Inject]// Injects "getMySQLConnection" because it's set as primary// #[Inject("getMariaDBConnection")]// or #[Inject(qualifier: "getMySQLConnection")]privatePDO$db;publicfunctiongetUserById(int$id):array {$stmt =$db->prepare('SELECT * FROM users WHERE id=?');$stmt->execute([$id]);return$stmt->fetch(); }}
<?phpuseSyringe\Syringe;useSyringe\Repository\ComponentRepository;$repo =newComponentRepository();$repo->addConfiguration(DbConfiguration::class);// $repo->addConfiguration(OtherConfiguration::class);Syringe::initialize($repo);$testClass = Syringe::new(TestClass::class);$user =$testClass->getUserById(1);var_dump($user);// ...