ãŠã§ãéçºã®äžçã§ã¯ãPHPã®éèŠæ§ã¯è°è«ã®äœå°ããããŸãããéç¿å¿ é ã®ããžãã¹ããŒãœã³åãã«ãPHPã®åºæ¬ããå¿çšãŸã§ãæ軜ã«ç解ã§ããã¬ã€ãããæäŸããŸããPHPãäœã§ãããããå§ãŸãããã®æŽå²çèæ¯ãå¯èœãªæ©èœããããŠææ°ããŒãžã§ã³ã®ç¹åŸŽã«è³ããŸã§ã®ã«ãã¬ããžãããã«ãå®éã®éçºç°å¢æ§ç¯ããåºæ¬ææ³ãããã°ã©ãã³ã°å®è·µããããŠã»ãã¥ãªãã£ãŸã§ã®å®çšçã¢ããã€ã¹ããã¬ãŒã ã¯ãŒã¯ãCMSã®è§£èª¬ãå¿ããã«ãçŸä»£ã®PHPéçºãç·æ¬ããæ å ±ãæºèŒã§ãããã®ãªãŒãæã§ã¯ãåµæ¥ããã®é²åãããžãã¹å®åãžã®å¿çšããããŠåžžã«å€ããæè¡ç°å¢ã«é©å¿ããããã®ãªãœãŒã¹ãæ¢ãæ ã«ãèªãããŸãã
1. PHPã®æŠèŠ
1.1 PHPãšã¯äœãïŒ
PHPã¯ããµãŒããŒãµã€ãã§åäœããã¹ã¯ãªããèšèªã§ãããäž»ã«ãŠã§ãéçºã«ãŠå©çšãããŠããŸãããã®ç¹åŸŽã¯ãHTMLã³ãŒãã«åã蟌ã圢ã§äœ¿çšã§ããããã«ããåçãªããŒãžã®äœæãå¯èœãšãªããŸããPHPã®ã³ãŒãããµãŒããŒäžã§è§£éãããã¯ã©ã€ã¢ã³ããžã¯ãã®çµæã®ã¿ãéä¿¡ãããããããšã³ããŠãŒã¶ãŒã«ã¯ãã®ããã»ã¹ãèŠããŸããã
PHPã¯ãã®ã·ã³ã¿ãã¯ã¹ïŒæ§æïŒãCèšèªã«äŒŒãŠãããJavaScriptãJavaãšãã£ãä»ã®ããã°ã©ãã³ã°èšèªã®éçºè ã«ãšã£ãŠã芪ãã¿ãããã§ãããŸããããŒã¿ããŒã¹ãšã®é£æºãåŸæã§ãMySQLãªã©ã®ããŒã¿ããŒã¹ç®¡çã·ã¹ãã ãšçµã¿åãããŠçšããããããšãå€ãã§ãã
ããã«PHPã¯ãªãŒãã³ãœãŒã¹ã§ãããç¡åã§å©çšå¯èœãªããšããäžçäžã«åºãŸããŸããããã®çµæã倧èŠæš¡ãªã³ãã¥ããã£ã圢æãããè±å¯ãªããã¥ã¡ã³ããŒã·ã§ã³ãšãµããŒããæäŸãããŠããŸãã
1.2 PHPã®æŽå²çèæ¯
PHPã¯1994幎ã«Rasmus Lerdorfæ°ã«ãã£ãŠéçºãããåœåã¯ãPersonal Home Page Toolsãã®ç¥ã ã£ãããšãååã®ç±æ¥ã§ããããã¯åœŒèªèº«ã®ãŠã§ãããŒãžã®èšªåè ã®è¿œè·¡çšã®äžé£ã®ã¹ã¯ãªããããå§ãŸããŸããã
PHPã¯é«éã«ããŒãžã§ã³ã¢ãããç¶ããã³ãã¥ããã£ã®è²¢ç®ã«ããããŸããŸãªæ¡åŒµæ©èœãå®çŸãããã»ããããã©ãŒãã³ã¹ãšã»ãã¥ãªãã£ãåäžããŸãããããã«ãããPHPã¯å人ã®ãããžã§ã¯ããããšã³ã¿ãŒãã©ã€ãºã¬ãã«ã®ãŠã§ãã¢ããªã±ãŒã·ã§ã³ãŸã§å¹ åºã䜿ãããããã«ãªããŸããã
ã€ãã·ã£ã«ãªãªãŒã¹ããé·ã幎æãçµãŠãPHPã¯ãŠã§ãéçºãã©ãããã©ãŒã ãšããŠã®å°äœã確ç«ããå€ãã®æåãªã³ã³ãã³ã管çã·ã¹ãã ïŒCMSïŒãäŸãã°WordPressãDrupalãªã©ãPHPããŒã¹ã®ãããžã§ã¯ããçãŸããŸããã
1.3 PHPã§ã§ããããš
PHPã§å®çŸã§ãããããžã§ã¯ãã®ç¯å²ã¯éåžžã«åºããã·ã³ãã«ãªãŠã§ããµã€ãããè€éãªãŠã§ãããŒã¹ã®ã¢ããªã±ãŒã·ã§ã³ããªã³ã©ã€ã³ã·ã§ããã³ã°ã«ãŒããã³ã³ãã³ã管çã·ã¹ãã ãAPIsãªã©ããããŸãããšãããããŠã§ããµã€ãã®ããã¯ãšã³ãéçºã«ãããŠã¯æ¬ ãããªãèšèªã§ãããšèšããã§ãããã
ããã«PHPã¯ãã©ãŒã å ¥åã®åŠçããã¡ã€ã«ã®æäœãã»ãã·ã§ã³ç®¡çãã¡ãŒã«éä¿¡ãªã©ããŠã§ãã¢ããªã±ãŒã·ã§ã³éçºã«å¿ èŠãªããŸããŸãªæ©èœããã€ãã£ããµããŒãããŠããŸããããã«ãããéçºè ã¯è€éãªæ©èœãç°¡åã«å®è£ ããããšãã§ããŸãã
ãŸããå€æ°ã®ãã¬ãŒã ã¯ãŒã¯ãååšããŠãããLaravelãSymfonyãªã©ã®ãã¬ãŒã ã¯ãŒã¯ã䜿çšããããšã§ãéçºå·¥çšãå éãããããå¹ççãªã³ãŒãã£ã³ã°ãè¡ããããã«ãªããŸãã
1.4 PHPã®ããŒãžã§ã³é²å
PHPã®åæã®ããŒãžã§ã³ããçŸåšã«è³ããŸã§ãPHPã¯ãããŸã¬é²åãéããŠããŸããPHP7ãå ¬éãããæãå€§å¹ ãªããã©ãŒãã³ã¹ã®åäžãèŠãããå€ãã®æ°æ©èœãè¿œå ãããŸãããããã«ããPHPã¯åäžä»£ã®ããŒãžã§ã³ã«æ¯ã¹ãŠå€§ããæ§èœãæ¹åãããŸããã
PHPã®ããŒãžã§ã³ãé²åããæ¯ã«ãæ°ããªæ¹åç¹ãåãå ¥ããããäžæ¹ã§ãå€ãæ©èœãã»ãã¥ãªãã£ãªã¹ã¯ãå«ãèŠçŽ ã¯éæšå¥šãšãªããæçµçã«ã¯åé€ãããããšããããŸãããã®ãããéçºè ã¯åžžã«æ°ããPHPã®ããŒãžã§ã³ã«æ³šæãæãå¿ èŠããããŸãã
PHPã¯ããŒãžã§ã³8ã§ããã«å€ãã®åŒ·åãªæ°æ©èœãšåã·ã¹ãã ã®æ¹è¯ãå°å ¥ãããéçºè ã³ãã¥ããã£ããé«ãè©äŸ¡ãããŠããŸããèšèªæ©èœãæŽç·ŽãããªãããPHPã¯åŒãç¶ããŠã§ãéçºã®ããã®ãªãŒãããã°ã©ãã³ã°èšèªã§ããç¶ããŠããŸãã
2. PHPç°å¢ã®èšå®
PHPãçšããéçºãå§ããã«ããããæåã®ã¹ãããã¯ç°å¢ã®èšå®ã§ãããã®ããã»ã¹ã¯ãéçºè ãã¹ã ãŒãºã«ã³ãŒããèšè¿°ãããã¹ããè¡ããããã«åºç€ãæŽããããã«äžå¯æ¬ ã§ãã
以äžã«ãPHPãã¯ãããšããéçºç°å¢ã®æ§ç¯ã«åœ¹ç«ã€åºæ¬çãªæé ã解説ããŸããããã«ãããPHPã®ãã£ãããªãã£ããã«ã«æŽ»çšããå¹ççãªéçºã¯ãŒã¯ãããŒãæ§ç¯ããããšãã§ããŸãã
ãããã®ã¬ã€ãã©ã€ã³ã«åŸããæ軜ã«PHPç°å¢ãã»ããã¢ããããã³ãŒãã£ã³ã°ã®æ ãã¹ã¿ãŒãããŸãããã
2.1 éçºç°å¢ã®éžå®
æåã¯ãç°å¢æ§ç¯ã®ããã®é©åãªéçºç°å¢ãéžå®ããããšãéèŠã§ããããŸããŸãªãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ãããŒã«ãå©çšå¯èœã§ãããéžæã¯ãããžã§ã¯ãã®èŠä»¶ãå人ã®å¥œã¿ã«äŸåããŸãã
äžè¬çã«ãWindowsãMac OSãLinuxãåºã䜿çšãããŠããŸããåå¿è ã§ã䜿ããããXAMPPãMAMPãªã©ã®ãªãŒã«ã€ã³ã¯ã³ããã±ãŒãžã人æ°ããããŸãã
éžæããç°å¢ãå°æ¥ã®ãããžã§ã¯ããããŒã ã®ã³ã©ãã¬ãŒã·ã§ã³ã«ãé©ããŠããããšã確èªããããšã倧åã§ãã
2.2 PHPã®ã€ã³ã¹ããŒã«ãšèšå®
éžãã éçºç°å¢ã«å¿ããŠãPHPãã€ã³ã¹ããŒã«ããããã»ã¹ãç°ãªããŸããäžè¬ã«ãPHPã®å ¬åŒãµã€ãããå¿ èŠãªããŒãžã§ã³ãããŠã³ããŒãããæ瀺ã«åŸã£ãŠã€ã³ã¹ããŒã«ãé²ããŸãã
ãã®éçšã§ãphp.iniãã¡ã€ã«ã®èšå®ãè¡ãããšã倧åã§ãããã®ãã¡ã€ã«ã«ãã£ãŠãšã©ãŒãã°ã®èšå®ãã䜿çšããæ¡åŒµæ©èœã®æå¹åãªã©ãããŸããŸãªã«ã¹ã¿ãã€ãºãã§ããŸãã
ãŸããã³ãã³ãã©ã€ã³ãIDEã§PHPã®ããŒãžã§ã³ã確èªã§ããããã«ãç°å¢å€æ°ãžã®ãã¹ãèšå®ããããšãå¿ããªãã§ãã ããã
2.3 WebãµãŒããšã®é£æº
PHPã¯ãµãŒããŒãµã€ãã®ã¹ã¯ãªããèšèªã§ãããããWebãµãŒããšã®é£æºãå¿ é ã§ããApacheãNginxãäž»ãªéžæè¢ãšãªããŸãã
äžè¬ã«ãApacheã¯mod_phpã¢ãžã¥ãŒã«ã䜿çšããŠPHPãšé£æºããNginxã¯FastCGIãä»ããŠPHPãšããåãããŸããèšå®ã¯ç°ãªããŸãããææžåãããå€ãã®ã¬ã€ããå©çšè ããµããŒãããŸãã
WebãµãŒãã®èšå®ãè¡ããPHPãã¡ã€ã«ãæ£ããå®è¡ãããããšã確èªããããšã§ãæ¬æ Œçãªéçºã«å ¥ãåã®æºåãæŽããŸãã
2.4 éçºããŒã«ã®å°å ¥
PHPã®ã³ãŒãã£ã³ã°ã«ã¯ãå¹ççãªãšãã£ã¿ãŒãŸãã¯çµ±åéçºç°å¢(IDE)ã®å°å ¥ãæå¹ã§ãããããã®ããŒã«ã¯ãã·ã³ã¿ãã¯ã¹ã®ãã€ã©ã€ããã³ãŒãã®èªåè£å®ããããã°æ©èœãæäŸããéçºããã»ã¹ãã¹ããŒãã¢ããããŸãã
Visual Studio CodeãPHPStormãšãã£ãå€æ©èœãªIDEã¯ãPHPéçºè ã«ãšã£ãŠåŒ·åãªæ¯æŽããŒã«ãšãªããŸãããããã«ã¯ãã©ã°ã€ã³ãæ¡åŒµæ©èœãè±å¯ã«çšæãããŠããŸãã
æåŸã«ãããŒãžã§ã³ç®¡çã·ã¹ãã ãç¹ã«Gitã®äœ¿çšã¯ãã³ãŒãã®å€æŽã远跡ããããŒã ã§ã®ã³ã©ãã¬ãŒã·ã§ã³ãè¡ãäžã§éåžžã«æå¹ã§ãããããã䜿çšããããšã§ãããå¹ççãã€å®å šã«éçºãé²ããããšãå¯èœã«ãªããŸãã
3. PHPã®åºæ¬ææ³
3.1 å€æ°ãšããŒã¿å
PHPã«ãããå€æ°ã¯ã“$”èšå·ã«ç¶ããŠå€æ°åãèšè¿°ããããšã§äœæããŸããå€æ°åã¯åå¥çã«ã¯è±åãŸãã¯ã¢ã³ããŒã¹ã³ã¢ã§å§ããå¿ èŠãããããã®åŸã«è±æ°åãã¢ã³ããŒã¹ã³ã¢ãç¶ããããŸããäŸãã°ã$variableã$_myVariableãªã©ãæå¹ãªå€æ°åã§ãã
ããŒã¿åã«é¢ããŠPHPã¯å€æ§ãªåããµããŒãããŠãããäž»ãªãã®ã«ã¯æŽæ°(int)ãæµ®åå°æ°ç¹æ°(float)ãæåå(string)ãé å(array)ããªããžã§ã¯ã(object)ãnullããããŸããPHPã¯åçåä»ãèšèªã§ãããããå€æ°ã®åã¯èªåçã«æ±ºå®ãããå¿ èŠã«å¿ããŠåå€æãè¡ãããŸãã
PHPã«ã¯å®æ°ãããããã㯠define() é¢æ°ã䜿çšããã const ããŒã¯ãŒãã䜿çšããŠå®£èšããŸããå®æ°ã®ååã¯éåžžã倧æåã䜿çšããŠè¡šç€ºãããŸããäžåºŠèšå®ãããå®æ°ã®å€ã¯å€æŽããããšãã§ããŸããã
3.2 å¶åŸ¡æ§é ïŒæ¡ä»¶åå²ã»ç¹°ãè¿ãïŒ
æ¡ä»¶åå²ã«ã¯äž»ã«ifæã䜿ãããŸããifæã§ã¯ãæ¬åŒ§å ã«æå®ããæ¡ä»¶ãçã§ããã°å®è¡ããããããã¯ãå®çŸ©ããããšãã§ããŸããããã«ãelseãelseifãçšããŠæ¡ä»¶ãæ¡åŒµããããšãå¯èœã§ããswitchæããŸãæ¡ä»¶åå²ã«äœ¿çšãããäžã€ã®å€æ°ã®å€ã«åºã¥ããŠè€æ°ã®ã±ãŒã¹ãå€æã§ããŸãã
ç¹°ãè¿ãåŠçã«ã¯foræãwhileæãdo-whileæããããŸããforæã¯åæåãæ¡ä»¶ãå¢æžãšãã3ã€ã®èŠçŽ ã§ã«ãŒãã管çããŸããwhileæã¯æ¡ä»¶ãçã®éå®è¡ãç¶ããã·ã³ãã«ãªã«ãŒãã§ããdo-whileæã¯å°ãªããšãäžåºŠã¯ãããã¯å ã®ã³ãŒããå®è¡ããããšãä¿èšŒããŸãã
foreachæã¯é åããªããžã§ã¯ãã®åèŠçŽ ã«å¯Ÿããç¹°ãè¿ãåŠçã«ç¹åããŠããŸããåã€ãã¬ãŒã·ã§ã³ã«ãããŠãé åããã®çŸåšã®èŠçŽ ã®å€ãå€æ°ã«å²ãåœãŠãããŸããããã«ããé åã®æäœãéåžžã«ç°¡åã«ãªããŸãã
3.3 é¢æ°ã®äœ¿ãæ¹
PHPã§ã¯ãé¢æ°ãå©çšããŠã³ãŒããåå©çšå¯èœãªã»ã¯ã·ã§ã³ã«åå²ããããšãã§ããŸããé¢æ°ã¯functionããŒã¯ãŒãã§å®çŸ©ããç¶ããŠé¢æ°åãšã«ãã³å ã«ä»»æã®ãã©ã¡ãŒã¿ãèšè¿°ããŸããé¢æ°ã®æ¬äœã¯äžæ¬åŒ§ã§å²ã¿ãŸãã
é¢æ°ã¯å€ãè¿ãããšãã§ããreturnæãçšããŠçµæãè¿ããŸããvoidé¢æ°ã¯å€ãè¿ãããããã»ã¹ã®å®è¡ãå€ã®çæãªã©ã®å¯äœçšã®ã¿ãç®çãšããŠããŸããé¢æ°ã®ãã©ã¡ãŒã¿ã¯ããã©ã«ãå€ãæã€ããšãå¯èœã§ãããã«ããé¢æ°ã®æè»æ§ãé«ãŸããŸãã
å€æ°é¢æ°ãç¡åé¢æ°ãPHPã®å€æ§ãªæ©èœãšããŠå©çšã§ããŸããããã«ãããåçãªé¢æ°åŒã³åºããã³ãŒã«ããã¯ã®äœ¿çšãå¯èœã«ãªããPHPã®ããã°ã©ãã³ã°ãããè¡šçŸåè±ãã«ãªããŸãã
3.4 ãªããžã§ã¯ãæåã®åºç€
PHPã¯ãªããžã§ã¯ãæåããã°ã©ãã³ã°(OOP)ã«å¯Ÿå¿ããŠãããã¯ã©ã¹ãããããã£ãã¡ãœãããç¶æ¿ãéããŠãœãããŠã§ã¢èšèšãè¡ãããšãã§ããŸããã¯ã©ã¹ã¯æ°ããã¿ã€ããäœæãããã³ãã¬ãŒãã§ããããªããžã§ã¯ãã¯ã¯ã©ã¹ããäœæãããã€ã³ã¹ã¿ã³ã¹ã§ãã
ããããã£ã¯ãªããžã§ã¯ãã®ããŒã¿ãä¿æããã¡ãœããã¯ãªããžã§ã¯ãã«é¢é£ã¥ããããæ©èœãå®è¡ããŸããã¯ã©ã¹ã¯å¯èŠæ§ãæå®ããããšã«ãããpublicãprotectedãprivateããããã£ãã¡ãœãããæã€ããšãå¯èœã§ãã
ç¶æ¿ã¯ããã¯ã©ã¹ã®ããããã£ãã¡ãœãããå¥ã®ã¯ã©ã¹ãåŒãç¶ãä»çµã¿ã§ãããåå©çšæ§ãæ¡åŒµæ§ãé«ããŸããPHPã§ã¯ãabstractã¯ã©ã¹ãinterfaceã䜿çšããŠãå é²çãªãªããžã§ã¯ãæåã®ãã¿ãŒã³ãå®è£ ããããšãã§ããŸãã
4. PHPã§ã®ããã°ã©ãã³ã°å®è·µ
4.1 ç°¡åãªã¹ã¯ãªããã®äœæ
PHPã掻çšããããã°ã©ãã³ã°ã®ç¬¬äžæ©ã¯ãåºæ¬çãªã¹ã¯ãªããã®äœæããå§ãŸããŸãã’Hello, World!’ãšåºåããç°¡åãªã¹ã¯ãªããã¯ãååŠè ã«ãšã£ãŠPHPã®ææ³ãšæ§é ãç解ããã®ã«æé©ãªäŸã§ãã
ã¹ã¯ãªãããäœæããã«ã¯ãããã¹ããšãã£ã¿ã«ãšå ¥åãããã¡ã€ã«ã’.php’ã®æ¡åŒµåã§ä¿åããã ãã§ãããã®ã³ãŒãã¯ãWebãµãŒããŒäžã§åäœãããã©ãŠã¶ã«æååã衚瀺ããŸãã
å§ããåã«ãPHPããµãŒããŒã«ã€ã³ã¹ããŒã«ãããŠããããšã確èªããŸããããããã«ããšã©ãŒå ±åæ©èœã掻çšããŠãéçºéçšã§åé¡ãçºçããéã«ãããã°ã容æã«ããŸãã
4.2 ãã©ãŒã ããŒã¿ã®åŠç
ãã©ãŒã ããŒã¿ã®åŠçã¯Webã¢ããªã±ãŒã·ã§ã³éçºã«ãããŠéèŠãªèŠçŽ ã§ããPHPã䜿çšãããšããŠãŒã¶ãŒããåéããæ å ±ãç°¡åã«åãæ±ãããšãã§ããŸãã
HTMLã® ã䜿çšããŠããŒã¿ãåéããPHPã®$_POSTãŸãã¯$_GETã¹ãŒããŒã°ããŒãã«ãéããŠãããã®ããŒã¿ã«ã¢ã¯ã»ã¹ã§ããŸããã»ãã¥ãªãã£ã確ä¿ããããã«ããŠãŒã¶ãŒå ¥åã¯åžžã«æ€èšŒããé©åã«ãµãã¿ã€ãºããå¿ èŠããããŸãããã©ãŒã ããŒã¿ãåŠçããã«ã¯ããã£ãŒã«ãåãæ£ããèå¥ããããŒã¿ãé©åã«æ±ãããã®ããžãã¯ãæ§ç¯ããããšãéèŠã§ããããã«ããããŠãŒã¶ãŒäœéšãåäžãããä¿¡é Œã®ãããã¢ããªã±ãŒã·ã§ã³ãæ§ç¯ã§ããŸãã
4.3 ã»ãã·ã§ã³ãšã¯ãããŒã®ç®¡ç
ã»ãã·ã§ã³ãšã¯ãããŒã¯ãŠãŒã¶ãŒã®ç¶æ ããã³ã»ãã·ã§ã³æ å ±ã管çããäžã§äžå¯æ¬ ã§ããã»ãã·ã§ã³ã¯ãµãŒããŒåŽã§æ å ±ãä¿æããã¯ãããŒã¯ã¯ã©ã€ã¢ã³ãåŽã§ããŒã¿ãä¿åããŸãã
PHPã«ã¯ã»ãã·ã§ã³ç®¡çã容æã«ããããã®çµã¿èŸŒã¿é¢æ°ããããŸããsession_start()é¢æ°ãåŒã³åºããŠããã$_SESSIONã¹ãŒããŒã°ããŒãã«é åãéããŠæ å ±ãä¿åããã¢ã¯ã»ã¹ã§ããŸãã
å¯Ÿç §çã«ãsetcookie()é¢æ°ã䜿ã£ãŠã¯ãããŒãèšå®ããããšãã§ããŸããã¯ãããŒã«ã¯æå¹æéãèšããããšãå¯èœã§ãããããã«ãããŠãŒã¶ãŒããµã€ãã蚪ããéã®èšå®ããã°ã€ã³ç¶æ ãä¿æã§ããŸãã
4.4 ããŒã¿ããŒã¹ãšã®é£æº
æãäžè¬çãªPHPã®äœ¿çšæ³ã®äžã€ã¯ãããŒã¿ããŒã¹ãšã®é£æºã§ããMySQLãPostgreSQLãªã©ã®ããŒã¿ããŒã¹ãšçµã¿åãããããšã§ãåçãªWebã¢ããªã±ãŒã·ã§ã³ãäœæããããšãã§ããŸãã
PHPã«ã¯PDOãmysqliãšãã£ãããŒã¿ããŒã¹æœè±¡åã©ã€ãã©ãªãçšæãããŠããŸãããããã®ã©ã€ãã©ãªãå©çšããããšã§ãããŒã¿ããŒã¹ãžã®æ¥ç¶ãããŒã¿ã®ååŸãã¬ã³ãŒãã®æ¿å ¥ãæŽæ°ãç°¡æœã«è¡ããŸãã
å®å šãªããŒã¿ããŒã¹ã®æäœã«ã¯ãSQLã€ã³ãžã§ã¯ã·ã§ã³æ»æãé¿ããããã«ãããªãã¢ãã¹ããŒãã¡ã³ãããã€ã³ããã©ã¡ãŒã¿ã䜿çšããããšãäžå¯æ¬ ã§ããããã«ãããããŒã¿ããŒã¹ãžã®ã¯ãšãªãããå®å šãã€å¹ççã«è¡ãããšãå¯èœãšãªããŸãã
5. PHPã®å®å šæ§ãšããã©ãŒãã³ã¹
PHPã¯äžçäžã§åºã䜿çšããããµãŒããŒãµã€ãã¹ã¯ãªããèšèªã§ããåªããã³ã³ãã³ã管çã·ã¹ãã ããŠã§ãã¢ããªã±ãŒã·ã§ã³ãããã³å€åœ©ãªããã¯ãšã³ãæ©èœãžã®é©çšæ§ãæã¡ãŸããããã®å®å šæ§ãšããã©ãŒãã³ã¹ã«ã€ããŠã¯ãéçºè ã«ãšã£ãŠéèŠãªçŠç¹ãšãªãã¹ããããã¯ã§ãã
å®å šæ§ã®é æ ®ãªãã«ã¯ãæœåšçãªã»ãã¥ãªãã£è匱æ§ã«ãŠã§ãã¢ããªã±ãŒã·ã§ã³ãããããŠããŸãããŠãŒã¶ãŒããŒã¿ãäŒæ¥æ å ±ããªã¹ã¯ã«æãããšã«ãªããŸãããŸããããã©ãŒãã³ã¹ã«ãããŠããåªãããŠãŒã¶ãŒãšã¯ã¹ããªãšã³ã¹ãæäŸããããã«ã¯ãé©åãªæé©åãå¿ èŠäžå¯æ¬ ã§ãã
ãã®èšäºã§ã¯ãPHPã®å®å šæ§ãšããã©ãŒãã³ã¹åäžã«é¢ããããŒãã€ã³ãã解説ããæãäžè¬çãªåé¡ãå¹çè¯ã解決ããæ¹æ³ãåŠãã§ãããŸãããã
5.1 ã»ãã¥ãªãã£ã®ãã¹ããã©ã¯ãã£ã¹
PHPã¢ããªã±ãŒã·ã§ã³ã«ãããã»ãã¥ãªãã£ã¯æ¥µããŠéèŠã§ããSQLã€ã³ãžã§ã¯ã·ã§ã³ãã¯ãã¹ãµã€ãã¹ã¯ãªããã£ã³ã°ïŒXSSïŒãã¯ãã¹ãµã€ããªã¯ãšã¹ããã©ãŒãžã§ãªïŒCSRFïŒãªã©ãããŸããŸãªæ»æãé²ãã«ã¯ãã»ãã¥ãªãã£ã®ãã¹ããã©ã¯ãã£ã¹ãéµå®ããå¿ èŠããããŸãã
ãŸããå ¥åããŒã¿ã®ããªããŒã·ã§ã³ãšãµãã¿ã€ãºã¯åºæ¬äžã®åºæ¬ã§ããå€éšããã®ããŒã¿ã察象ã«ãæ³å®ããã圢åŒãšé©åããŠããããå³å¯ã«ãã§ãã¯ããäžæ£ãªã³ãŒããé€å»ããããšãå¿ é ã§ããããªãã¢ãã¹ããŒãã¡ã³ããšãã€ã³ãå€æ°ã®äœ¿çšã¯ãSQLã€ã³ãžã§ã¯ã·ã§ã³æ»æãé²ãã®ã«å¹æçã§ãã
ã»ãã·ã§ã³ç®¡çããŸãéèŠã§ãã»ãã·ã§ã³ãã€ãžã£ãã¯ãé²ãããã«ã»ãã¥ã¢ãªèšå®ãæœãã¹ãã§ãããŸããã»ãã¥ãªãã£æŽæ°ããªãªãŒã¹ãã次第ãè¿ éã«PHPããŒãžã§ã³ãã©ã€ãã©ãªã®ã¢ããããŒããè¡ããè匱æ§ããã·ã¹ãã ãä¿è·ããããšãæ¬ ãããŸããã
5.2 ããã©ãŒãã³ã¹ã®æé©å
PHPã¢ããªã±ãŒã·ã§ã³ã®ããã©ãŒãã³ã¹æ¹åã¯ããŠãŒã¶ãŒãšã¯ã¹ããªãšã³ã¹ãçŽæ¥åäžãããŸããåŠçé床ã®é ãã¯ãçŽã¡ã«é¢è±çã®å¢å ã«ã€ãªãããããæé©åã¯é¿ããŠéããªãéã§ãã
ãã£ãã·ã³ã°æŠç¥ãèæ ®ããããšã¯ãããã©ãŒãã³ã¹åäžã®ããã®æå¹ãªæ段ã§ããäŸãã°ããªãã³ãŒããã£ãã·ã³ã°ã¯PHPã³ãŒããã³ã³ãã€ã«ããåå©çšå¯èœãªåœ¢ã§ä¿åããããšã§ãã¹ã¯ãªããå®è¡æéãççž®ããŸãããŸããäžèŠãªããŒã¿ããŒã¹ã¢ã¯ã»ã¹ãæé€ããã¯ãšãªã®æé©åãè¡ãããšãéèŠã§ãã
ããã«ãéçãªãœãŒã¹ã«å¯ŸããŠã¯ããã©ãŠã¶ãã£ãã·ã³ã°ãå©çšããã³ã³ãã³ãããªããªãŒãããã¯ãŒã¯ïŒCDNïŒãçšããŠãªãœãŒã¹ã®é ä¿¡ãæ©ããæ¹æ³ããããŸããã¹ã¯ãªããã®ããŒãé åºããµã€ãºã®åæžããããŒãžããŒãæéã®åæžã«å¯äžããŸãã
5.3 ãšã©ãŒåŠçãšãããã°
PHPéçºã§ã¯ããšã©ãŒåŠçãšãããã°ã¯åæ»ãªéçºããã»ã¹ãšç¶æ管çã«å¿ èŠäžå¯æ¬ ã§ããé©åã«ãšã©ãŒããã³ããªã³ã°ããããšã§ãäžå ·åã®ããã³ãŒããæ©æ¥ã«ç¹å®ããä¿®æ£ãè¡ãããšãã§ããŸãã
éçºäžã«ã¯ãšã©ãŒã¬ããŒãã£ã³ã°ãå šãŠæå¹ã«ããæªå®çŸ©ã®å€æ°ãã€ã³ããã¯ã¹ããã®ä»ã®ããŒãã£ã¹ãèªèã§ããããã«ããŠãããšè¯ãã§ãããããŸããäŸå€åŠçãé©åã«è¡ããtry/catchæã䜿çšããŠãšã©ãŒãæãããã°ã«èšé²ããããšããšã©ãŒç®¡çã«ã¯äžå¯æ¬ ã§ãã
Xdebugãªã©ã®ãããã°ããŒã«ã䜿çšããããšã§ãã³ãŒãã®ã¹ãããã¹ã«ãŒããã¬ãŒã¯ãã€ã³ãèšå®ãªã©ã®ãããã°ãè¡ãããšãã§ããå¹ççã«åé¡ã解決ã§ããŸããã³ãŒãã®ãã¬ãŒã¹ããããã¡ã€ãªã³ã°ã«ãã£ãŠãããã©ãŒãã³ã¹ã®ããã«ããã¯ãç¹å®ããããšãå¯èœã§ãã
5.4 ã³ãŒãã®ã¡ã³ããã³ã¹ãšã¢ããããŒã
ã³ãŒãããŒã¹ã®å¥å šæ§ãä¿ã€ããã«ã¯ãå®æçãªã¡ã³ããã³ã¹ãšã¢ããããŒããå¿ èŠã§ããããã«ã¯ãã³ãŒãã®ãªãã¡ã¯ã¿ãªã³ã°ãå»æ¢ãããé¢æ°ã®çœ®ãæãããããŠæ°æ©èœãžã®é©å¿ãå«ãŸããŸãã
ã¯ãªãŒã³ãªã³ãŒããæžãããšã¯ãã¡ã³ããã³ã¹ã容æã«ããã ãã§ãªããä»ã®éçºè ãã³ãŒããç解ããããããããã«ãéèŠã§ããé¢æ°ãã¯ã©ã¹ã¯äžè²«æ§ããã£ãŠåœåããé©åãªã³ã¡ã³ããã¬ã®ã¥ã©ãŒã«èšè¿°ããããšã奚å±ãããŸãã
æåŸã«ãããŒãžã§ã³ç®¡çã·ã¹ãã ãå©çšããããšã§ãã³ãŒãã®å€æŽå±¥æŽã远跡ãããããªããå€äººæ°ã«ããéçºã§ãäžè²«æ§ãä¿ã¡ããããªããŸããGitãªã©ã®ããŒã«ã¯ã³ãŒãã®æ¹åã«ãããŠéèŠãªåœ¹å²ãæãããããè¯ãã³ãŒã管çãå®çŸããŸãã
6. PHPã®å ãè¡ã: ãã¬ãŒã ã¯ãŒã¯ãšCMS
PHPã¯ãŠã§ãéçºã«äžå¯æ¬ ãªèšèªã§ããããã®å¿çšç¯å²ãåºããããã«ãå€ãã®ãã¬ãŒã ã¯ãŒã¯ãšCMSãååšããŸãããããã®ããŒã«ã䜿ãããšã§ãéçºã®ã¹ããŒããäžããããã»ãã¥ã¢ã§ä¿å®ããããã¢ããªã±ãŒã·ã§ã³ãäœæã§ããŸãã
ãã¬ãŒã ã¯ãŒã¯ã¯ããããåºæ¬çãªæ©èœãåããéçºè ãçŽé¢ããäžè¬çãªèª²é¡ã«å¯ŸåŠããããã®ãœãªã¥ãŒã·ã§ã³ãæäŸããŸããäžæ¹ãCMSã¯ãŠã§ããµã€ãã®æ§ç¯ãšç®¡çãç°¡æåããéæè¡è ã§ãã³ã³ãã³ãã®æŽæ°ãã¡ã³ããã³ã¹ãè¡ããããæ¯æŽããŸãã
æ¬èšäºã§ã¯ãPHPéçºãå éããããã®ä»£è¡šçãªãã¬ãŒã ã¯ãŒã¯ãšCMSã玹ä»ããããããéžå®ããéã®èæ ®ç¹ãããã³PHPã³ãã¥ããã£ãšåŠç¿ãªãœãŒã¹ã«ã€ããŠèª¬æããŸãã
6.1 人æ°ã®PHPãã¬ãŒã ã¯ãŒã¯
Laravelã¯çŸåšãã£ãšã泚ç®ãããŠããPHPãã¬ãŒã ã¯ãŒã¯ã®äžã€ã§ãããã®ãšã¬ã¬ã³ããªæ§æãšå ç¢ãªæ©èœã»ããã¯éçºè ã®çç£æ§ã倧ãã«åäžãããŸããMVCã¢ãŒããã¯ãã£ãæ¡çšããŠãããã»ãã¥ãªãã£ããã£ãã·ã¥ãã«ãŒãã£ã³ã°ãšãã£ãé¢ã§åªããŠããŸãã
SymfonyããŸããé«åºŠãªã¢ãžã¥ã©ãŒã·ã¹ãã ãšåºç¯ãªã³ãã¥ããã£ãµããŒããèªããã¬ãŒã ã¯ãŒã¯ã§ããåå©çšå¯èœãªã³ã³ããŒãã³ãã«ããæè»æ§ãé«ããå€ãã®ãããžã§ã¯ãã§æ¡çšãããŠããŸãã
CodeIgniterã¯ãã·ã³ãã«ããšããã©ãŒãã³ã¹ã®ãã©ã³ã¹ãåªãããã¬ãŒã ã¯ãŒã¯ãšããŠäººæ°ãåããŠããŸããèšå®ãå°ãªããè¿ éãªéçºãå¯èœã§ãç¹ã«ã¹ã¢ãŒã«ããããã£ã¢ã ãªãããžã§ã¯ãã«é©ããŠããŸãã
6.2 ã³ã³ãã³ã管çã·ã¹ãã ïŒCMSïŒ
WordPressã¯ãäžçäžã§åºã䜿çšãããŠããCMSã§ãããã®äœ¿ãããããšã«ã¹ã¿ãã€ãºæ§ã«ãããããã°ããè€éãªããŒã¿ã«ãµã€ããŸã§ãããŸããŸãªçš®é¡ã®ãŠã§ããµã€ãã«å¯Ÿå¿ããŠããŸãã
Drupalã¯åŒ·åã§æè»æ§ã®é«ãCMSãšããŠãäŒæ¥ã¬ãã«ã®ãããžã§ã¯ãã«ããå©çšãããŠããŸããåªããã»ãã¥ãªãã£ãšã¢ãžã¥ãŒã«æ§ã«ãããé«åºŠãªã«ã¹ã¿ãã€ãŒãŒã·ã§ã³ãå¯èœã§ãã
Joomlaãåœéçã«å€ãã®æ¯æãéããCMSã§ãäžçšåºŠã®è€éæ§ã®ãŠã§ããµã€ãã«ç¹ã«é©ããŠããŸããè±å¯ãªæ¡åŒµæ©èœãšçŽæçãªç®¡çã€ã³ã¿ãŒãã§ãŒã¹ãé åã§ãã
6.3 ãã¬ãŒã ã¯ãŒã¯éžå®ã®èæ ®ç¹
ãã¬ãŒã ã¯ãŒã¯ãéžã¶éã«ã¯ããããžã§ã¯ãã®ãµã€ãºãããŒã ã®ã¹ãã«ã¬ãã«ããããŠå¿ èŠãšãããæ©èœã«æ³šæãæãå¿ èŠããããŸããå°èŠæš¡ãªãããžã§ã¯ãã§ã¯è»œéãªãã¬ãŒã ã¯ãŒã¯ãéžæãããããšãå€ãã§ãã
ãŸãããããžã§ã¯ãã®é·æçãªã¡ã³ããã³ã¹ãšé²åãèæ ®ããå Žåãã¢ã¯ãã£ããªã³ãã¥ããã£ãšè¯å¥œãªããã¥ã¡ã³ããŒã·ã§ã³ãæã€ãã¬ãŒã ã¯ãŒã¯ãéžã¶ããšãéèŠã§ãããã¬ãŒã ã¯ãŒã¯ã®å°æ¥çãªã¢ããããŒãããµããŒãäœå¶ãèæ ®ããã¹ãç¹ã§ãã
ã»ãã¥ãªãã£ã®åŽé¢ãç¡èŠã§ããŸãããã¯ãã¹ãµã€ãã¹ã¯ãªããã£ã³ã°æ»æãSQLã€ã³ãžã§ã¯ã·ã§ã³ãªã©ã®è åšã«å¯Ÿå¿ã§ããã»ãã¥ãªãã£æ©èœãçµã¿èŸŒãŸããŠãããã©ããã確èªããããšãäžå¯æ¬ ã§ãã
6.4 åŠç¿ãªãœãŒã¹ãšã³ãã¥ããã£
PHPãã¬ãŒã ã¯ãŒã¯ãCMSãç¿åŸããããã«ã¯ã質ã®é«ãåŠç¿ãªãœãŒã¹ã®å©çšãéåžžã«å¹æçã§ãããªã³ã©ã€ã³ãã¥ãŒããªã¢ã«ããã©ãŒã©ã ãã³ãŒã¹ãªã©ããããåå¿è ããäžçŽè ãŸã§åœ¹ç«ã€æ å ±ãåŸãããŸãã
倧ããªãã¬ãŒã ã¯ãŒã¯ãCMSã«ã¯ããããç¬èªã®ã³ãã¥ããã£ãååšãããŠãŒã¶ãŒå士ã§å©ãåããç¥èãå ±æããå Žæãšãªã£ãŠããŸãããããã®ã³ãã¥ããã£ã掻çšããããšã§ãã³ãŒãã®åé¡è§£æ±ºããã¹ããã©ã¯ãã£ã¹ã«ã€ããŠåŠã¶ããšãã§ããŸãã
æåŸã«ãéçºè ã€ãã³ããã«ã³ãã¡ã¬ã³ã¹ãžã®åå ããæè¡ããã¬ã³ãã«å¯Ÿããæ·±ãç解ãšæ°ããªãããã¯ãŒã¯ã®æ§ç¯ã«ã€ãªãããŸããã€ãã«æ°ããããšãåŠã³ãã¢ããããŒããç¶ããæèããPHPã®äžçã§å ãè¡ãéçºè ã«ãªãããã«ã¯æ¬ ãããªãèŠçŽ ã§ãã
7. ãŸãšã
Webéçºã®å¿èéšãšããŠç¥ããããPHPãã玹ä»ããŸããå ¥éè ãããã£ãªã¢ã¢ãããå³ãããžãã¹ããŒãœã³ã«æé©ãªè§£èª¬ã§ããPHPã¯ãµãŒããŒãµã€ãã§åäœããã¹ã¯ãªããèšèªã§ã1995幎ã«ç»å ŽããŠä»¥æ¥ãeã³ããŒã¹ãµã€ããããã°ãªã©ã«äžå¯æ¬ ãªååšãšãªã£ãŠããŸãããã®èšèªã¯ãã·ã³ãã«ãªããã°ããè€éãªäŒæ¥ãµã€ããŸã§æ§ç¯å¯èœã§ãããŒãžã§ã³ã¢ããããšã«ã»ãã¥ãªãã£ãæ©èœãåäžããŠããŸããç°å¢æ§ç¯ã«ã¯ãé©åãªéçºç°å¢ã®éžå®ãšãWebãµãŒããšã®é£æºãéèŠã§ãã<>åºæ¬ææ³ã¯ãå€æ°ãããŒã¿åãå¶åŸ¡æ§é ãããªããžã§ã¯ãæåãŸã§å¹ åºããåå¿è ã§ã容æã«ã¹ã¯ãªããäœæãããŒã¿ããŒã¹é£æºãå¯èœã§ããã»ãã¥ãªãã£é¢ã§ã¯ãããŒã¿ä¿è·ãšé«éåŠçãéèŠãããšã©ãŒåŠçãé©åã«è¡ãããšãå¿æããŸããããããã«PHPã¯ãã¬ãŒã ã¯ãŒã¯ãCMSãšãšãã«é²åãç¶ããéçºã®çç£æ§ãé£èºçã«é«ããŠããŸããè±å¯ãªåŠç¿ãªãœãŒã¹ãšæŽ»çºãªã³ãã¥ããã£ããåŠç¿ãšå°å ¥ã®å©ããšãªãã§ãããã