PHPã¯ãå人ã®ããŒã ããŒãžããŒãžããFacebookãVimeoãWordPressãããã«ã¯YouPornã®ãããªãã¹ããã³ãŸã§ãå€ãã®Webãããžã§ã¯ãã綎ãæã人æ°ã®ããããã°ã©ãã³ã°èšèªã®1ã€ã§ããããšã¯èª°ããç¥ã£ãŠãããšæããŸãã
PHPã¯1995幎ã«ç»å ŽããŸããããOOPã®å®å šãªãµããŒãã¯2005幎ã«ãªãªãŒã¹ãããPHP5ã§ã®ã¿å®è£ ãããŸããã ãã®éã倧éã®ã³ãŒããäœæãããŸããããè¯ããã®ãæªããã®ããéåžžã«æ代é ãã§ä¿å®ãå°é£ãªãã®ããããŸããã
PHPãšã³ã·ã¹ãã å šäœã®ãããªå€ãã®ãããžã§ã¯ãã¯ãçŸåšã掻æ°ã«æºã¡ãéœåžãããã¯ã®å€èŠ³ãè¡šããŠããŸãã

æ°ããPHPããŒãžã§ã³
æ°ãã10幎ã§PHP5.3ãšPHP5.4ãçãŸããŸãããçç£æ§ã®åäžã«å ããŠãå€ãã®å©ç¹ããããŸãã ãããã®ã»ãã®äžéšã次ã«ç€ºããŸãã
1. åå空é ïŒphp 5.3ïŒ
ï¿Œnamespace JoliCode\Conferences; use General\Talk\Session; class SymfonyLive extends Session { public function runPresentation() { } }
- ã¯ã©ã¹ã®åœåã®å¶ç¶ããä¿åããŸãã
- äžç¢ºå®æ§ãæžãããŸãã
- é·ãã¯ã©ã¹ã®åœåãé¿ããŸãã
2. å¿åé¢æ°ãšã¯ããŒãžã£ãŒ ïŒphp 5.3ïŒãããã«ã¯ãªããžã§ã¯ãå ïŒphp 5.4ïŒ
$container['session'] = function ($c) { return new Session($c['session_storage']); };
3. Pharã¢ãŒã«ã€ã ïŒphp 5.3ïŒ
1ã€ã®æ¥ç¶ãããpharãã¡ã€ã«ã®åœ¢åŒã§ã©ã€ãã©ãªãé åžã§ããŸãã
4. ãgotoã -äœã.. .. ?? ïŒPHP 5.3ïŒ
5. ç¹æ§ ïŒphp 5.4ïŒ
6.é åã宣èšããããã®çãæ§æïŒ ïŒphp 5.4ïŒ
ᅩfunction fruits() { return ['apple', 'banana', 'orange']; } echo fruits()[0]; // Outputs: apple ᅩ
ãªã©ãªã©ã
ãã¬ãŒã ã¯ãŒã¯
PHPã®æ°ããããŒãžã§ã³ã¯ããã¬ãŒã ã¯ãŒã¯ã®åºçŸã®è§Šåªãšãªã£ãŠããŸãã ãã®äžã§ããsymfonyãZend FrameworkãYiiãCodeIgniterãCakePHPã«èšåãã䟡å€ããããŸãã ãããã®ã»ãšãã©ãã¹ãŠãããªãªãŒã¹ããã2çªç®ã®ããŒãžã§ã³ã§å€§å¹ ã«æŽæ°ãããŸããã
ãããã®ãã¬ãŒã ã¯ãŒã¯ã®1ã€ã§ããSymfony2ã¯ã äžé£ã®ç¬ç«ããã³ã³ããŒãã³ããšãPHPèšèªã®æ°æ©èœã䜿çšããå€ããªã£ãã³ãŒãã§ãããžã§ã¯ãã埩掻ãããã®ã«åœ¹ç«ã€å€ãã®ãµãŒãããŒãã£ãœãªã¥ãŒã·ã§ã³ã§æ§ç¯ãããŠããŸãã
ãããžã§ã¯ãèçæŠç¥
ããªãã®ãããžã§ã¯ãã5幎éç¶ããŠãããšæ³åããŠãã ããã ãã®æéäžïŒ
- 倧éã®æ©èœãå®è£ ãããŠããŸãã
- ãããžã§ã¯ãã¯æ¬çªç°å¢ã§åäœããŸãã
- å€ãã®ãŠãŒã¶ãŒãåžžã«äœ¿çšããŠããŸãã
- ããå€ãã®ããŒã¿ãèç©ãããŸããã
- çµéšè±å¯ãªããŒã ããããžã§ã¯ãã®åšãã«åœ¢æãããŸããã
ãã®å ŽåïŒ
- ãã£ãããšæ°ããæ©èœãå°å ¥ããŸãã
- ãããžã§ã¯ããšãã®ã€ã³ãã©ã¹ãã©ã¯ãã£ã®ãµããŒãã«é¢ããåé¡ã
- æ¢åã®æ©èœãæ¹åããããšã¯å°é£ã§ãã

æ¹æ³1ïŒããã°ãã³
ãã®ãããªç¶æ³ã§æãèªç¶ãªæ¬²æ±ã¯ã ãã¹ãŠã®ã³ãŒããæšãŠãŠããããåã³æžãçŽãããšã§ãïŒ ãã®ããã«è¡ã£ããšæ³åããŠãã ããã ééããªããç§ãã¡ã¯æ°ããã¢ãã³ãªãã¬ãŒã ã¯ãŒã¯ã®1ã€ãšé£æºããæ©äŒããæºè¶³ãåŸãŸããå€ãã³ãŒããå¿ããæªã倢ã®ããã«ã³ãŒãããããã¯ããå ŽæããããŸãã
ããããçå®ã«çŽé¢ããå Žåãããã»ã¹ã¯éåžžã«å€§èŠæš¡ã§è€éã§é·ããªãå¯èœæ§ãé«ãããã¹ãŠã®æ©èœãæžãæããã«ã¯å°ãªããšã1人幎ããããŸãã åæã«ããããžã§ã¯ãã®æ¢åããŒãžã§ã³ã®éçºãåæ¢ããŸããããã«ããããããžã§ã¯ãã®ããžãã¹ã«çµæžçæ倱ãçããååãªæ³šæãæãããŠããªããŠãŒã¶ãŒãæµåºããŸãã ãŸãããã¹ãŠã®æ©èœãæ°ãããã©ãããã©ãŒã ã«å®å šã«ç§»è¡ã§ããªããšãããªã¹ã¯ãé«ããªããŸãã ãããã®ãªã¹ã¯ã®çç±ã®1ã€ã¯ãæ¢åã®ããŒã¿ãããŒã¿ã¹ãã¬ãŒãžã·ã¹ãã ã®æ°ããæ§é ã«ç§»è¡ããæ段ãéçºããå¿ èŠãããããšã§ãã
æ¹æ³2ïŒäžè²«æ§ãä¿ã€
ãããã£ãŠããããã³ã°ãã䟡å€ã¯ãããŸããããããžã§ã¯ãã段éçã«æŽæ°ããæŠç¥ã¯ããåççã§ãã åæã«ããããžã§ã¯ãã®éçºãšãµããŒãã¯åæ¢ããªããããããžãã¹ã¯åæ¢ããŸããã ãã®ã¢ãããŒãã§ã¯ããããžã§ã¯ãã§æ°ãããã¬ãŒã ã¯ãŒã¯å šäœã䜿çšããããšã¯ã§ããŸããããåã ã®éšåã䜿çšã§ããŸãã ãã®å Žåã®Symfony2ã³ã³ããŒãã³ãã¯éåžžã«é©ããŠããŸãã äŸåé¢ä¿ã¯ãããŸãããããã€ã³ãã¢ã³ããã€ã³ãæ©èœãæäŸããŸãã
è¡åã«ç§»ã
ã¹ããã1.ãããžã§ã¯ããPHP 5.3ãŸãã¯PHP 5.4ã«è»¢éãã

ãŸããPHPãå°ãªããšãPHPããŒãžã§ã³5.3ã«æŽæ°ããŸãã ãããæ éã«è¡ããŸããæåã«dev-serverã§æŽæ°ãå®è¡ããã·ã¹ãã ã®ã©ã®éšåãèœã¡ãŠããªãããšã確èªããŸãã ãã®åŸãprodãµãŒããŒã§ç¢ºèªã§ããŸãã åæã«ããããžã§ã¯ãå šäœã§ã¯ãªãA / Bãã¹ãã¢ãŒãã§ãããè¡ãããšããå§ãããŸããããã«ãããäžéšã®ãŠãŒã¶ãŒã®ã¿ãæ°ããç°å¢ã«ã¢ã¯ã»ã¹ã§ããŸãã
ãã¹ãŠãæ£åžžã«æ©èœããŠãããšç¢ºä¿¡ããããå ã«é²ãããšãã§ããŸãã
ã¹ããã2.å°æ¥ã®ã·ã¹ãã ãªãã¡ã¯ã¿ãªã³ã°ã®åºç€ãæºåãã

äž»èŠã³ã³ããŒãã³ãã§ããClassLoaderãDependencyInjectionãããã³HTTPFoundationãæ¥ç¶ããŸãã ãããã¯ãªãã¡ã¯ã¿ãªã³ã°ãæ¯æŽããæ°ããã³ã³ããŒãã³ããæ¥ç¶ããããã®åºç€ãšãªããŸãã
ãããã®ã³ã³ããŒãã³ãããã³ä»¥äžã®ãã¹ãŠã®ã³ã³ããŒãã³ãã¯ã Composerããã±ãŒãžãããŒãžã£ãŒã䜿çšããŠç°¡åã«æ¥ç¶ã§ããŸãã ãµãŒãããŒãã£ã©ã€ãã©ãªäžã®ãããžã§ã¯ãã®äŸåé¢ä¿ãjson圢åŒã§ç°¡æœã«èšè¿°ããããããèªåçã«ã€ã³ã¹ããŒã«ããã³æŽæ°ãããããžã§ã¯ãã«æ¥ç¶ããããã®èªåããŒããã¡ã€ã«ãçæããããšãã§ããŸãã Habréã«ã¯ããã®äœ¿çšæ¹æ³ã«é¢ããåªããèšäºããããŸãã
ãã®å ŽåãComposerã®æ§æãã¡ã€ã«ã¯æ¬¡ã®ããã«ãªããŸãã
ᅩ{ "autoload": { "psr-0": {} }, "name": "xavierlacot/blog", "description": "My old blog on steroids", "require": { "php": ">=5.3.3", "symfony/class-loader": "2.1.*", "symfony/dependency-injection": "2.1.*", "symfony/http-foundation": "2.1.*" } }
ã³ã³ããŒãã³ããã€ã³ã¹ããŒã«ããŸãã
ᅩ$ php composer.phar install Installing dependencies from lock file - Updating symfony/class-loader (dev-master)
ãããŠããããããããžã§ã¯ãã«æ¥ç¶ããŸãã
ᅩ<?php // Composer require 'vendor/autoload.php';
ããã§äœ¿çšã§ããŸãïŒ
ã¯ã©ã¹ããŒããŒã ã¯ã©ã¹èµ·åæšæº
1幎ååãPHPã³ãã¥ããã£ã¯ããã€ãã®å¥çŽãæ¡æããŸããããã®ãã¡ã®1ã€ã§ããPSR-0å¥çŽã¯ ãçŸä»£ã®ã¯ã©ã¹ãªãŒãããŒããŒãšã®äºææ§ã«é¢ããã«ãŒã«ãšã³ãŒãèŠä»¶ã説æããŠããŸãïŒãªãŒãããŒããŒã®æŽå²ã¯ãããšããã§ããæžãããŠããŸã ïŒ
èŠããã«ããã®æšæºã§ã¯æ¬¡ã®2çš®é¡ã®ã¯ã©ã¹åœåãèš±å¯ãããŠããŸãã
åå空éçµç±ïŒçŸä»£çãªã¢ãããŒãïŒ
ï¿Œclass: Symfony\Component\HttpFoundation\Request path: vendor/src/Symfony/Component/HttpFoundation/Request.php
ã¢ã³ããŒã¹ã³ã¢ã䜿çšããŠïŒPEARæšæºãå»æ¢ãããã¢ãããŒãïŒ
ï¿Œclass: Twig_Extension_Core path: vendor/twig/lib/Twig/Extension/Core.php
ãã®å Žåãã¯ã©ã¹ãã¡ã€ã«ãæ¥ç¶ããããšããªãŒãããŒããŒã¯èªåçã«ã¹ã©ãã·ã¥ïŒ\ïŒãšã¢ã³ããŒã¹ã³ã¢ïŒ_ïŒããã£ã¬ã¯ããªã»ãã¬ãŒã¿ãŒïŒ/ïŒã«çœ®ãæãã.phpæ¡åŒµåãæåŸã«è¿œå ããŸãã
ClassLoaderã³ã³ããŒãã³ãã䜿çšãããšãPSR-0å¥çŽã«åŸã£ãŠã¯ã©ã¹ã®ãªãŒãããŒããæŽçã§ããŸã ã
ã¹ããã3.ã¯ã©ã¹ã®ãªãã¡ã¯ã¿ãªã³ã°ãšClassLoaderã®äœ¿çš
ããšãã°ãLDAPãæäœããããã®ç¹å®ã®ã¯ã©ã¹ããããŸãã
ᅩ<?php class prefixedLdapConnector { public function __construct() { // - } }
PSR-0æšæºã«æºæ ããŠããŸãã
ᅩ<?php namespace Prefix\Ldap\Client; class Client { public function __construct() { // - } }
ClassLoaderã«Prefixãããã¬ãã«ã¹ããŒã¹ãç»é²ããŸãã
ï¿Œuse Symfony\Component\ClassLoader\UniversalClassLoader; $loader = new UniversalClassLoader(); $loader->registerNamespace('Prefix', __DIR__ . '/src/'); $loader->register();
ã³ãŒãã§Prefix \ Ldap \ Clientã¯ã©ã¹ã䜿çšãããšãClassLoaderãèªåèªã¿èŸŒã¿ãåŠçããŸãã
// ClassLoader use Prefix\Ldap\Client; $client = new Client();
ã¹ããã4.èŠæ±åŠçã®ãªãã¡ã¯ã¿ãªã³ã°/å¿çã®æºåã HttpFoundationã䜿çšãã

HTTPFoundationã³ã³ããŒãã³ãã¯ãçä¿¡èŠæ±ã®åŠçãšãµãŒããŒå¿çã®æºåãç°¡çŽ åããŸãã
ãããžã§ã¯ãã«2è¡ãè¿œå ããŸãã
// ClassLoader, ï¿Œuse Symfony\Component\HttpFoundation\Request; // Request HTTPFoundation $request = Request::createFromGlobals();
çä¿¡ãªã¯ãšã¹ãã«é¢ãããã¹ãŠãç¥ã£ãŠãã$ãªã¯ãšã¹ããªããžã§ã¯ããååŸããŸãã
- $ request-> request replace $ _POST
- $ request-> queryã¯$ _GETã眮ãæããŸã
- $ request-> Cookieã$ _COOKIEã眮ãæããŸã
- ãªã©
ãããŠãå¿çãšåæ§ã«ïŒ
ᅩuse Symfony\Component\HttpFoundation\Response; $response = new Response(); ᅩ$response->setContent('This will be the response content'); $response->headers->set('Content-Type', 'text/html'); $response->setStatusCode(200); // Response $response->prepare($request); $response->send();
ãªãéåžžã®PHPå€æ°ãšã¡ãœãããæ°ããã¯ã©ã¹ã«çœ®ãæããã®ããšæãããŸãã å®éããã®ã¢ãããŒãã«ã¯å€ãã®å©ç¹ããããŸãã ãŸãããªããžã§ã¯ãã®åœ¢åŒã§æ瀺ãããèŠæ±ããã³å¿çããŒã¿ãæäœããããã®ãããç解ããããã¬ãã«ã®æœè±¡åãå ¥åã§ããŸãã ãããžã§ã¯ãã®ä»»æã®é åããã®èŠæ±ãšå¿çãå®å šã«å¶åŸ¡ã§ããŸãã 第äºã«ããã®ã³ã³ããŒãã³ãã¯å€ãã®ç¬ç«ããéçºè ã«ãã£ãŠãã¹ããããŠãã ãã»ãã¥ãªãã£èŠä»¶ã«æºæ ããŠããŸãã 3çªç®ã«ãHTTPFoundationã䜿çšããã³ãŒãã¯ãã¹ããã¯ããã«ç°¡åã§ãã
ã¹ããã5.ãããžã§ã¯ãã®æ©èœããµãŒãã¹ã«èšèšããŸãã äŸåæ§æ³šå ¥ã³ã³ããŒãã³ã
ã»ãšãã©ãã¹ãŠã®ãããžã§ã¯ãã«ã¯ãããŸããŸãªã©ã€ãã©ãªãã¯ã©ã¹ããŸãã¯é©åãªããã°ã©ã é¢æ°ãæ åœããå€ãã®é¢æ°ããããŸãã äŸåæ§æ³šå ¥ã³ã³ããŒãã³ãã䜿çšãããšããã®æ©èœãŸãã¯ãã®æ©èœãå®è£ ãããªããžã§ã¯ããäœæããã³æ§æããããã»ã¹ãæŽçããã³éäžåã§ããŸãã ãã®ãããªãªããžã§ã¯ãã«ã¯ãéµäŸ¿ãµãŒãã¹ãããŒã¿ããŒã¹ãããŒãžã£ãŒããã³ãã¬ãŒããšã³ãžã³ãªã©ãå«ãŸããŸãã

ã¡ãŒã«ãéä¿¡ããããã®ã¯ã©ã¹ããããšããŸãããïŒ
ᅩ<?php namespace Prefix\Mailer; class Mailer { $prefix = ''; public function send($to, $subject, $body) { // } }
ãããŠãã³ãŒãã§ã¯ãã¡ãŒã«ã¯æ¬¡ã®ããã«éä¿¡ãããŸãã
ï¿Œfunction saveUser($name, $email) { $mailer = new Mailer(); $mailer->send($user->getEmail(), 'Welcome ' . $user->getName(), 'A test'); }
ãªãã¡ã¯ã¿ãªã³ã°ãå°ãè¡ãããµãŒãã¹ã³ã³ãããäœæããŠããã®äžã«éµäŸ¿ã¯ã©ã¹ãç»é²ããŸãããã
ï¿Œuse Symfony\Component\DependencyInjection\ContainerBuilder; $container = new ContainerBuilder(); $container->register('mailer', 'Mailer');
æåãéä¿¡ããè¡ã§2çªç®ã®å°ããªãªãã¡ã¯ã¿ãªã³ã°ãè¡ã£ãŠã¿ãŸãããã
ï¿Œfunction saveUser($name, $email) { // stuff here $mailer = $this->container->get('mailer'); $mailer->send($user->getEmail(), 'Welcome '.$user->getName(), 'A test'); }
ãããã®å°ããªå€æŽã«ãããäžåºŠã«ããã€ãã®ããŒãã¹ãä»äžãããŸãã
- ãããžã§ã¯ãã®ä»ã®å Žæã®ãµãŒãã¹ã³ã³ãããããªããžã§ã¯ããååŸããŠããMailerã¯ã©ã¹ã®ãªããžã§ã¯ãã¯1åã ãäœæãããŸãã
- ã¡ãŒã©ãŒãµãŒãã¹ãäžå çã«æ§æã§ããããã«ãªããŸããã
- å¥ã®ãµãŒãã¹ã«ç°¡åã«çœ®ãæããããšãã§ããŸãã
åæ§ã«ããããžã§ã¯ãã©ã€ãã©ãªã次ã ã«ãµãŒãã¹ãšããŠæå®ãããã¹ãŠã®äœæ¥ããµãŒãã¹ã³ãŒã«ã§æŽçã§ããŸãã
ã¹ããã6.次ã¯ïŒ
Symfony2ã®æåã®3ã€ã®ã³ã³ããŒãã³ããæ¥ç¶ããããšã«ãããåãã¢ã«ãŽãªãºã ã䜿çšããŠä»ã®åçã«æçšãªã³ã³ããŒãã³ãã®äœ¿çšãéå§ã§ããŸã ã
- ã³ã³ãœãŒã« -ã³ã³ãœãŒã«ã¹ã¯ãªããã®éçºïŒã³ãã³ãã©ã€ã³ãŸãã¯cronããåŒã³åºãããŸãïŒ
- Finder-ãã¡ã€ã«ãšãã©ã«ããŒã®æ€çŽ¢
- ã«ãŒãã£ã³ã°-HTTPãªã¯ãšã¹ããšèšå®å¯èœãªãã©ã¡ãŒã¿ãŒã®ã»ããéã®å¯Ÿå¿ãèšå®ã§ããŸã
- ãã³ãã¬ãŒã -ãããžã§ã¯ãã§ãã³ãã¬ãŒããæäœããããã«å¿ èŠãªãã¹ãŠãæäŸããŸã
- ãã®ä»ã
äžæ©äžæ©ãã³ã³ããŒãã³ãããšã«ãããªãã®ãããžã§ã¯ãã¯æ¬¡ç¬¬ã«å€åããæ ¹æ¬çãªæ±ºå®ãªãã«æ°ããç掻ãå§ããŸãã é 匵ã£ãŠãªãã¡ã¯ã¿ãªã³ã°ãã楜ãã¿ãã ããïŒ