<?phpclassTodo{/** * @var PDO */private$pdo;/** * @param PDO $pdo */publicfunction__construct(PDO$pdo){$this->pdo=$pdo;}/** * @param string $todo things to do */publicfunctionadd($todo){$stmt=$this->pdo->prepare('INSERT INTO TODO (todo) VALUES (:todo)');$stmt->bindParam(':todo',$todo);$stmt->execute();}}$pdo=newPDO('mysql:dbname=test;host=localhost');$todo=newTodo($pdo);$todo->add('Get laundry');
WelcometotheComposerconfiggeneratorThiscommandwillguideyouthroughcreatingyourcomposer.jsonconfig.Packagename(<vendor>/<name>)[akihito/ray.tutorial]:Description[]:Author[AkihitoKoriyama<akihito.koriyama@gmail.com>]:MinimumStability[]:License[]:Defineyourdependencies.Wouldyouliketodefineyourdependencies(require)interactively[yes]?Searchforapackage[]:ray/diFound15packagesmatchingray/di[0]ray/di[1]ray/aop[2]jms/di-extra-bundle[3]aura/di[4]orno/di[5]league/di[6]mnapoli/php-di[7]zendframework/zend-di[8]mnapoli/php-di-zf1[9]ocramius/ocra-di-compiler[10]lcobucci/di-builder[11]aimfeld/di-wrapper[12]kdyby/autowired[13]seiffert/console-extra-bundle[14]vojtech-dobes/extensions-listEnterpackage# to add, or the complete package name if it is not listed []: 0Entertheversionconstrainttorequire[]:*Searchforapackage[]:Wouldyouliketodefineyourdevdependencies(require-dev)interactively[yes]?Searchforapackage[]:{"name":"akihito/ray.tutorial","require":{"ray/di":"*"},"authors":[{"name":"Akihito Koriyama","email":"akihito.koriyama@gmail.com"}]}Doyouconfirmgeneration[yes]?</akihito></name></vendor>
入力の必要な質問はこれだけでした。
123
Searchforapackage[]:ray/diEnterpackage# to add, or the complete package name if it is not listed []: 0Entertheversionconstrainttorequire[]:*
<?phpuseDoctrine\Common\Annotations\AnnotationRegistry;useRay\Di\AbstractModule;useRay\Di\Injector;useRay\Di\Di\Inject;useRay\Di\Di\Named;$loader=requiredirname(__DIR__).'/vendor/autoload.php';AnnotationRegistry::registerLoader([$loader,'loadClass']);classTodo{private$pdo;/** * @Inject */publicfunction__construct(PDO$pdo){$this->pdo=$pdo;}/** * @param $todo */publicfunctionadd($todo){$stmt=$this->pdo->prepare('INSERT INTO TODO (todo) VALUES (:todo)');$stmt->bindParam(':todo',$todo);$stmt->execute();}}classModuleextendsAbstractModule{publicfunctionconfigure(){$pdo=newPDO('mysql:dbname=test;host=localhost');$this->bind('PDO')->toInstance($pdo);}}$injector=Injector::create([newModule]);$todo=$injector->getInstance('Todo');/** @var $todo Todo */$todo->add('Walking in Ray');