ã¯ããã«
ãã®èšäºãèªãã§ããã®ã§ããã°ãPHPèšèªã®æ¡åŒµæ©èœãäœæããããšã«èå³ãããå¯èœæ§ãé«ãã§ãããã ããã§ãªãå Žå...ãããããã®èšäºãèªãã ãšããããªãã¯ããããçããªãã£ããšããäºå®ã«ãããããããããªãã¯ããªãèªèº«ã«ãã®èå³ãçºèŠããã§ãããïŒ
ãã®èšäºã§çŽ¹ä»ããè³æã¯ãPHPèšèªèªäœãšPHPã€ã³ã¿ãŒããªã¿ãŒã®äœæèšèªã§ããCã®äž¡æ¹ã«ç²ŸéããŠããããšãæå³ããŸãã
PHPã®æ¡åŒµæ©èœãäœæããçç±ãå®çŸ©ããããšããå§ããŸãããã
- èšèªã§åãå ¥ããããæœè±¡åã¬ãã«ã®ããã«ãPHPããçŽæ¥äœæã§ããªãã©ã€ãã©ãªãŸãã¯ç¹å®ã®OSåŒã³åºãã®ååšã
- PHPãéæšæºçãªæ¹æ³ã§åäœããããå Žåã
- PHPã§äœæããããœãªã¥ãŒã·ã§ã³ã¯æ¢ã«ãããŸãããããã»ã¹ã®é«éåãã³ã³ãã¯ãåãããã³ã¡ã¢ãªæ¶è²»ã®åæžãå®çŸã§ããããšã¯ãåãã§ãããã
- 販売ãããç¹å¥ãªã³ãŒãããããŸãã ãã ããè³Œå ¥è ãã³ãŒããå®è¡ã§ããããšã¯éèŠã§ããããœãŒã¹ãèŠãããšã¯ã§ããŸããã
äžèšã®çç±ã¯ãã¹ãŠååã§ãããæ¡åŒµæ©èœãäœæãããšãã¯ããŸããããäž»ã«æ¡åŒµæ©èœã§ããããšãç解ããå¿ èŠããããŸãã
æ¡åŒµæ©èœãšã¯äœã§ããïŒ
PHPã䜿çšããå¿ èŠãããå Žåã¯ãæ¡åŒµæ©èœã䜿çšããŸããã ããã€ãã®äŸå€ãé€ããPHPèšèªã§äœ¿çšå¯èœãªãã¹ãŠã®é¢æ°ã¯ç¹å®ã®æ¡åŒµæ©èœã«ã°ã«ãŒãåãããŸãã ã»ãšãã©ã®æ©èœïŒ400以äžïŒã¯ã æšæºã®æ¡åŒµæ©èœã®äžéšã§ãã PHPã®ãœãŒã¹ã³ãŒãã¯ãããããçŽ30åã®é¢æ°ãå«ãçŽ86åã®æ¡åŒµæ©èœãšãšãã«é åžãããŸãã ã«ãŠã³ããããšãåèšã§çŽ2500åã®é¢æ°ãåŸãããŸãã ããã ãã§ã¯äžååãªå ŽåãPECLãªããžããªã¯100ãè¶ ããè¿œå ã®æ¡åŒµæ©èœãæäŸããŸãããã€ã³ã¿ãŒãããã®åºå€§ãªå°åã«ã¯ããã«å€ãã®æ¡åŒµæ©èœããããŸãã
ããŸããããã ãå€ãã®æ©èœãæ¡åŒµæ©èœã«çµã¿èŸŒãŸããŠããã®ã«ãããã¯æ¡åŒµæ©èœã®å€ã«æ®ã£ãŠããŸããïŒã ãæ¡åŒµæ©èœãšã¯äœã§ããïŒã PHPã®äžæ žã¯äœã§ããïŒã
PHPã®ã³ã¢ã¯ã2ã€ã®ç¬ç«ããéšåã®åœ¢ã§å®è£ ãããŸãã èšèªã®æè¡çãªéšåã¯ã Zend Engine ïŒZEïŒã®åœ¢åŒã§æ瀺ãããŸãã ZEã¯ã人éãèªããã¹ã¯ãªãããã³ã³ãã¥ãŒã¿ãŒã䜿ããããããŒã¯ã³ã«å€æããŠããå®è¡ããŸãã ããã«ãZEã¯ã¡ã¢ãªç®¡çãå€æ°ã®ç¯å²ãããã³é¢æ°åŒã³åºãã®åŠçãæ åœããŸãã
ã«ãŒãã«ã®2çªç®ã®éšåã¯ãçŽæ¥ãã³ã¢ããšåŒã°ãããã®ïŒ PHPã³ã¢ ïŒã§ãã SAPIã¬ã€ã€ãŒïŒãµãŒããŒã¢ããªã±ãŒã·ã§ã³ããã°ã©ãã³ã°ã€ã³ã¿ãŒãã§ã€ã¹ãPHPã€ã³ã¿ãŒãã§ã€ã¹ãšä»ã®ãµãŒããŒãœãããŠã§ã¢-CLIãCGIãApacheãªã©ïŒãšã®ããåããæ åœããŸãã ããã«ãã«ãŒãã«ã¯safe_modeããã³open_basedirãã§ãã¯çšã®äžè¬åãããå¶åŸ¡å±€ïŒãããã®æ©èœã¯ããŒãžã§ã³5.3以éå»æ¢ããããšå®£èšãããŠããŸãïŒãããã³ãã¡ã€ã«ããã³ãããã¯ãŒã¯I / Oæäœãfopenãfreadãfwriteé¢æ°ã«é¢é£ä»ããã¹ããªãŒã å±€ãå®è£ ããŠããŸãã
ã©ã€ããµã€ã¯ã«
ç¹å®ã®SAPIãèµ·åãããšïŒããšãã°ãã³ãã³ã/ usr / local / apache / bin / apachectl startã䜿çšããŠApacheãµãŒããŒãèµ·åããå ŽåïŒãPHPã¯ã«ãŒãã«ãµãã·ã¹ãã ãèµ·åããŠäœæ¥ãéå§ããŸãã èµ·åæé ã®çµããã«åãã£ãŠãåæ¡åŒµæ©èœã®ã³ãŒããããŠã³ããŒããã ã¢ãžã¥ãŒã«åæå ïŒMINITïŒé¢æ°ãåŒã³åºããŸãã ããã«ãããåæ¡åŒµæ©èœã¯ãå éšå€æ°ãåæåãããªãœãŒã¹ã«ã¡ã¢ãªãå²ãåœãŠããªãœãŒã¹ãã³ãã©ãŒãšãã®æ©èœãZEã«ç»é²ã§ãããããã¹ã¯ãªããããã®æ¡åŒµæ©èœã®é¢æ°ãåŒã³åºããšãZEã¯å®è¡ããã³ãŒããèªèã§ããŸãã
次ã«ãPHPã¯SAPIã¬ã€ã€ãŒããã®ããŒãžåŠçèŠæ±ãäºæããŸãã CGIãŸãã¯CLI SAPIã®å Žåãããã¯ããã«äžåºŠã ãçºçããŸãã SAPI ApacheãIISããŸãã¯ä»ã®æ¬æ ŒçãªWebãµãŒããŒã®å Žåããªã¢ãŒããŠãŒã¶ãŒãïŒç«¶åããå¯èœæ§ãããïŒããŒãžãèŠæ±ãããã³ã«ãããŒãžåŠçã®èŠæ±ãçºçããŸãã ãã ãããªã¯ãšã¹ãã®éä¿¡æ¹æ³ã«é¢ä¿ãªãããã®åŠçã¯ãPHPã³ã¢ãZEã«ã¹ã¯ãªãããå®è¡ããããã®ç°å¢ãæ§æããããã«èŠæ±ã ãåæ¡åŒµæ©èœã®ãªã¯ãšã¹ãåæå ïŒRINITïŒé¢æ°ãåŒã³åºãããšããå§ãŸããŸãã RINITã¯ãç¹å®ã®ç°å¢å€æ°ãæ§æããç¹å®ã®èŠæ±ãªãœãŒã¹ã«ã¡ã¢ãªãå²ãåœãŠãä»ã®ã¿ã¹ã¯ãå®è¡ããæ©èœãæ¡åŒµæ©èœã«äžããŸãã åäœäžã®RINITé¢æ°ã®å¥œäŸã¯ã»ãã·ã§ã³æ¡åŒµã§ããsession.auto_startãæå¹ã«ãªã£ãŠããå ŽåãRINITé¢æ°ã¯èªåçã«session_starté¢æ°ãåŒã³åºãã $ _SESSIONå€æ°ãåæåããŸãã
ãªã¯ãšã¹ããåæåããããšãZEã¯PHPã¹ã¯ãªãããããŒã¯ã³ã«å€æãã次ã«å®è¡å¯èœãªãªãã³ãŒãã«å€æããŸãã ãããã®ãªãã³ãŒãã®ãããããæ¡åŒµæ©èœããã®é¢æ°åŒã³åºããèŠæ±ããå ŽåãZEã¯ãã®é¢æ°ãåŒã³åºãããã®åŒæ°ãçæããå®äºãããŸã§äžæçã«å¶åŸ¡ã転éããŸãã
ã¹ã¯ãªããã®å®è¡ãå®äºãããšãPHPã¯æ¡åŒµæ©èœããšã«ã·ã£ããããŠã³èŠæ± ïŒRSHUTDOWNïŒé¢æ°ãåŒã³åºããŸããããã¯ãã¯ãªãŒãã³ã°ããã»ã¹ãå®äºããããã«å¿ èŠã§ãïŒããšãã°ãã»ãã·ã§ã³å€æ°ããã£ã¹ã¯ã«ä¿åããïŒã 次ã®ã¹ãããã§ããZEã¯ã¯ãªãŒãã³ã°ããã»ã¹ïŒã¬ããŒãžã³ã¬ã¯ã·ã§ã³ãšãåŒã°ããŸãïŒãå®è¡ããå®è¡ãããã¹ã¯ãªããã§äœ¿çšãããåå€æ°ã«å¯ŸããŠunsetã¡ãœãããå®éã«å®è¡ããŸãïŒPHP 5.3以éãã¬ããŒãžã³ã¬ã¯ã·ã§ã³ã¡ã«ããºã ãå€§å¹ ã«æ¹åãããŸããïŒã
ãªã¯ãšã¹ãã®åŠçãå®äºãããšãPHPã¯SAPIããå¥ã®ã¹ã¯ãªãããåŠçãããªã¯ãšã¹ãããŸãã¯å®äºããä¿¡å·ãäºæããŸãã CGIãŸãã¯CLI SAPIã®å Žåãã次ã®èŠæ±ãã¯äžå¯èœã§ãããããSAPIã¯PHPã®ã·ã£ããããŠã³ãããã«éå§ããŸãã å®äºæã«ãPHPã¯ãã¹ãŠã®æ¡åŒµæ©èœãå埩åŠçããããããã«å¯ŸããŠã¢ãžã¥ãŒã«ã·ã£ããããŠã³ ïŒMSHUTDOWNïŒé¢æ°ãåŒã³åºããŸãããã®åŸãç¬èªã®ã«ãŒãã«ãµãã·ã¹ãã ãå®äºããŸãã
ãã®ããã»ã¹ã¯æåã¯å°ãæ··ä¹±ããŠããããã«èŠãããããããŸããããæ¡åŒµæ©èœã®äœæ¥ã«å ¥ããšãåŸã ã«ãããæããŸãã
ã¡ã¢ãªå²ãåœãŠ
äžå®å šã«èšè¿°ãããæ¡åŒµæ©èœã§ã®ã¡ã¢ãªãªãŒã¯ãåé¿ããããã«ãZEã¯è¿œå ãã©ã°ã«åºã¥ããŠç¬èªã®å éšã¡ã¢ãªç®¡çã¡ã«ããºã ã䜿çšããŠãããŒã¿ã®æå¹æéã決å®ããŸãã æ°žç¶çïŒ æ°žç¶ç ïŒã¡ã¢ãªå²ãåœãŠã¯ãåäžããŒãžèŠæ±ã®åŠçäžãããå€ãã®ã¡ã¢ãªãå²ãåœãŠãããããšãæå³ããŸãã ã¡ã¢ãªã®éæ°žç¶çãªå²ãåœãŠã¯ã解æŸé¢æ°ãåŒã³åºããããã©ããã«é¢ä¿ãªããèŠæ±ã®åŠçåŸã«ã¡ã¢ãªã解æŸããããšãæå³ããŸãã ããšãã°ããŠãŒã¶ãŒå€æ°ãžã®ã¡ã¢ãªã®å²ãåœãŠã¯æ¬è³ªçã«äžè²«æ§ããããŸããããªã¯ãšã¹ãã®åŠçãå®äºãããšããŠãŒã¶ãŒå€æ°ã䜿çšã§ããªããªãããã§ãã
çè«äžã®æ¡åŒµã§ã¯ãåãªã¯ãšã¹ãã®æåŸã«æ®çºæ§ã¡ã¢ãªãèªåçã«è§£æŸããããšãZEã«ä»»ããå¯èœæ§ããããšããäºå®ã«ãããããããããã¯æšå¥šãããŸããã å²ãåœãŠãããã¡ã¢ãªã¯é·ãéèŠæ±ãããªããŸãŸã«ãªãããã®ã¡ã¢ãªã«é¢é£ä»ãããããªãœãŒã¹ãæ£ããéããããå¯èœæ§ãäœããªããæçµçã«ãã¡ã¢ãªã®è§£æŸã§æ··ä¹±ãåŒãèµ·ããããšã¯æªãç¿æ £ã§ãã åŸã§èŠãããã«ããã¹ãŠã®ããŒã¿ãæ£ããã¯ãªãŒã³ã¢ãããããŠããããšã確èªããã®ã¯ç°¡åã§ãã
åŸæ¥ã®ã¡ã¢ãªå²ãåœãŠé¢æ°ïŒå€éšã©ã€ãã©ãªã䜿çšããå Žåã«ã®ã¿äœ¿çšããå¿ èŠããããŸãïŒãšãPHP / ZEã®å®æ°ããã³éæ°žç¶ã¡ã¢ãªå²ãåœãŠã®é¢æ°ãç°¡åã«æ¯èŒããŠã¿ãŸãããã
ãã©ãã£ã·ã§ãã« | æ°ãŸãã | åžžèš |
---|---|---|
mallocïŒã«ãŠã³ãïŒ | emallocïŒã«ãŠã³ãïŒ | pemallocïŒã«ãŠã³ãã1ïŒ * |
callocïŒã«ãŠã³ããæ°å€ïŒ | ecallocïŒã«ãŠã³ãã1ïŒ | pecallocïŒã«ãŠã³ããæ°å€ã1ïŒ |
strdupïŒstrïŒ | estrdupïŒstrïŒ | pestrdupïŒstrã1ïŒ |
strndupïŒstrãlenïŒ | estrndupïŒstrãlenïŒ | pemallocïŒïŒããã³memcpyïŒïŒ |
ç¡æïŒptrïŒ | efreeïŒptrïŒ | pefreeïŒptrã1ïŒ |
reallocïŒptrãnewsizeïŒ | ereallocïŒptrãnewsizeïŒ | pereallocïŒptrãnewsizeã1ïŒ |
mallocïŒcount * num + extrïŒ ** | safe_emallocïŒcountãnumãextrïŒ | safe_pemallocïŒcountãnumãextrïŒ |
äŸïŒemallocïŒ1234ïŒã¯ãpemallocïŒ1234ã0ïŒãšåãã§ã
** safe_emallocïŒïŒããã³ïŒPHP 5ïŒsafe_pemallocã¯ãæŽæ°ãªãŒããŒãããŒã®è¿œå ãã§ãã¯ãå®è£ ããŸãã
ç°å¢ã®ã»ããã¢ãããšæ§ç¯
PHPãšZend Engineã®çè«ã«ç²Ÿéããã®ã§ãä»äºã«åãæãããäœããå§ããã®ãåŸ ã€ããšã¯ã§ããŸããã ãã ãããã®åã«ãããã€ãã®ãã«ãããŒã«ãå ¥æããç®çã«åãããŠç°å¢ãæ§æããå¿ èŠããããŸãã
ãŸããPHPèªäœãšãPHPã«å¿ èŠãªãã«ãããŒã«ã®ã»ãããå¿ èŠã§ãã PHPããœãŒã¹ãããã«ãããå¿ èŠããªãå Žåã¯ã ãã®èšäºãã芧ã«ãªãããšããå§ãããŸãã PHPãœãŒã¹ã§ãã€ããªããã±ãŒãžã䜿çšããã®ã¯é åçãããããŸãããããã®ãããªã¢ã»ã³ããªã«ã¯å€ãã®å Žåãéçºããã»ã¹äžã«éåžžã«åœ¹ç«ã€2ã€ã®éèŠãªããã°ã©ã ãã©ã¡ãŒã¿ãŒ./configureããããŸããã 1ã€ç®ã¯enable-debugã§ãã ãã®ãªãã·ã§ã³ã¯ãã»ã°ã¡ã³ããŒã·ã§ã³ãšã©ãŒïŒsegfaultïŒãçºçãããšãã«ã«ãŒãã«ãã³ããååŸããgdbãããã¬ãŒã䜿çšããŠãšã©ãŒãçºçããå Žæãšçç±ã確èªã§ããããã«ãPHPãå®è¡å¯èœãã¡ã€ã«ã®è¿œå ãããã°æ å ±ã§ã³ã³ãã€ã«ããŸãã
2çªç®ã®ãªãã·ã§ã³ã®ååã¯ã䜿çšããPHPã®ããŒãžã§ã³ã«ãã£ãŠç°ãªããŸãã PHP 4.3ã§ã¯ãPHP 5ãã--enable-experimental-ztsãšåŒã°ãã --enable-maintainer-ztsã«ååãå€æŽãããŸãã ã ãã®ãªãã·ã§ã³ã䜿çšãããšãPHPã¯ãã«ãã¹ã¬ããç°å¢ã§åäœãããšèŠãªããã¹ã¬ããã¬ã¹ç°å¢ã§ã¯èŠããªãäžè¬çãªãšã©ãŒããã£ããã§ããŸããããã«ãã¹ã¬ããç°å¢ã§ã¯æ¡åŒµæ©èœã®åäœãäžå®å®ã«ãªããŸãã
è¿œå ãªãã·ã§ã³ã䜿çšããŠPHPãã³ã³ãã€ã«ããéçºãµãŒããŒïŒãŸãã¯ã¯ãŒã¯ã¹ããŒã·ã§ã³ïŒã«ã€ã³ã¹ããŒã«ãããšãæåã®æ¡åŒµæ©èœãäœæãããšããã¢ã€ãã¢ãæã¡ç Žãããšãã§ããŸãã
ãããŒã¯ãŒã«ã
ãã§ã«å¿ é ã®Hello Worldããã°ã©ã ã®äŸããªããã°ãããã°ã©ãã³ã°ã«é¢ããã©ã®å ¥éèšäºãå®æãããããšãã§ããŸããïŒ ãã®å ŽåããHello Worldããšããåèªãå«ãæååãè¿ã1ã€ã®é¢æ°ãPHPã«è¿œå ããæ¡åŒµæ©èœãäœæããããšãæ€èšããŸãã PHPã§ã¯ããããã次ã®ããã«å®è£ ããŸãã
<?php
function hello_world() {
return 'Hello World' ;
}
?>
次ã«ããã®é¢æ°ããPHPæ¡åŒµæ©èœãäœæããŸãããã ãŸããPHPãœãŒã¹ãã£ã¬ã¯ããªããªãŒã®ext /ãã£ã¬ã¯ããªã«helloãµããã£ã¬ã¯ããªãäœæããããã«cdãäœæããŸãã ååãšããŠããã®ãã£ã¬ã¯ããªã¯ãœãŒã¹ã®ãããã£ã¬ã¯ããªããªãŒã®å éšãŸãã¯å€éšã®ã©ãã«ã§ãé 眮ã§ããŸãããä»åŸèšäºãããªã³ã¯ã§ããããã«ããã«é 眮ããå¿ èŠããããŸãã ããã§ã¯ã3ã€ã®ãã¡ã€ã«ãäœæããå¿ èŠããããŸãã
- phpizeãŠãŒãã£ãªãã£ãã³ã³ãã€ã«çšã®æ¡åŒµæ©èœãæºåããããã«äœ¿çšããæ§æãã¡ã€ã«ïŒ config.m4 ïŒïŒ
PHP_ARG_ENABLE(hello, whether to enable Hello World support,
[ --enable-hello Enable Hello World support])
if test "$PHP_HELLO" = "yes"; then
AC_DEFINE(HAVE_HELLO, 1, [Whether you have Hello World])
PHP_NEW_EXTENSION(hello, hello.c, $ext_shared)
fi
- æ¡åŒµæ©èœãããŠã³ããŒãããããã«PHPã䜿çšããåœä»€ãå«ãããããŒãã¡ã€ã«ïŒ php_hello.h ïŒïŒ
#ifndef PHP_HELLO_H
#define PHP_HELLO_H 1
#define PHP_HELLO_WORLD_VERSION "1.0"
#define PHP_HELLO_WORLD_EXTNAME "hello"
PHP_FUNCTION(hello_world);
extern zend_module_entry hello_module_entry;
#define phpext_hello_ptr &hello_module_entry
#endif
- hello_worldé¢æ°ã®æ¬äœãå«ããœãŒã¹ãã¡ã€ã«ïŒ hello.c ïŒïŒ
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_hello.h"
static function_entry hello_functions[] = {
PHP_FE(hello_world, NULL)
{NULL, NULL, NULL}
};
zend_module_entry hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_HELLO_WORLD_EXTNAME,
hello_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
#if ZEND_MODULE_API_NO >= 20010901
PHP_HELLO_WORLD_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_HELLO
ZEND_GET_MODULE(hello)
#endif
PHP_FUNCTION(hello_world)
{
RETURN_STRING( "Hello World" , 1);
}
- hello_worldãšããé¢æ°ã宣èšãã
- æååãHello Worldããæ»ãå€ãšããŠèšå®
- ããŒã... 1ïŒ ãã®1ã¯ã©ãããæå³ã§ããïŒ
ZEã¯ãã¹ã¯ãªããã®å®äºæã«ãã¹ãŠã®èŠæ±ããããªãœãŒã¹ã解æŸãããããšã確èªããé«åºŠãªã¡ã¢ãªç®¡çã¡ã«ããºã ã䜿çšããããšãæãåºããŠãã ããã ã¡ã¢ãªç®¡çã®äžçã§ã¯ãåãã¡ã¢ãªãããã¯ãåã³è§£æŸããããšã¯å€§ããªah-ahãšèŠãªãããŸãã ãã®ã¢ã¯ã·ã§ã³ã¯ãããã«ãªãªãŒã¹ããšåŒã°ããã»ã°ã¡ã³ããŒã·ã§ã³ãšã©ãŒã®äžè¬çãªåå ã§ããããã¯ãããã°ã©ã ãããã¯ãæå±ããŠããªãã¡ã¢ãªãããã¯ã«ã¢ã¯ã»ã¹ããããšããããã§ãã
ç°¡åã«èšãã°ãZEãéçæååïŒãã®äŸã§ã¯ãHello WorldãïŒã«ãã£ãŠå æãããŠããã¡ã¢ãªã®è§£æŸãéå§ããªãããã«ãhello_worldé¢æ°ã¯åŒã³åºããããã³ã«æååã®ã³ããŒãè¿ãå¿ èŠããããŸãã
ååãšããŠã RETURN_STRINGé¢æ°ã¯ãæž¡ãããè¡ãã³ããŒããå¿ èŠããããšããäºå®ã«è²¬ä»»ãæã€ããšãã§ãããããåŸã§ãã®ã¡ã¢ãªãå®å šã«è§£æŸã§ããŸãã ãã ããæååã«ã¡ã¢ãªãå²ãåœãŠãæååãã³ããŒããçµæãšããŠã³ããŒãè¿ãããšã¯å éšZEé¢æ°ã®æšæºçãªåäœã§ã¯ãªãããã RETURN_STRINGã䜿çšãããšã2çªç®ã®åŒæ°ã䜿çšããŠå€æ°ãã³ããŒãããã©ãããæå®ã§ããŸã ã ãã®æŠå¿µãããèŠèŠçã«èª¬æããããã«ãæ©èœçã«ã¯åã®ãã®ãšåãã§ãã次ã®ã³ãŒããã©ã°ã¡ã³ãã«æ³šæããŠãã ããã
PHP_FUNCTION(hello_world)
{
char *str;
str = estrdup( "Hello World" );
RETURN_STRING(str, 0);
}
ãã®äŸã§ã¯ããHello Worldãè¡ã«æ®çºæ§ã¡ã¢ãªãæåã§å²ãåœãŠãŸãããããã¯ãRETURN_STRING é¢æ°ã䜿çšããŠåŒã³åºãã¹ã¯ãªããã«è¿ããã2çªç®ã®ãã©ã¡ãŒã¿ãŒãšããŠ0ãæå®ããŸãã
æ¡åŒµã¢ã»ã³ããª
ãã®æŒç¿ã®æåŸã®æé ã¯ãåçã«ããŒããããã¢ãžã¥ãŒã«ãšããŠæ¡åŒµæ©èœãæ§ç¯ããããšã§ãã åã®äŸãæ£ããã³ããŒããå Žåãext / hello /ãã£ã¬ã¯ããªãã3ã€ã®ã³ãã³ããå®è¡ããã ãã§ãã
$ phpize
$ ./configure --enable-hello
$ make
phpizeãŠãŒãã£ãªãã£ãæ£ããæ©èœããã«ã¯ã m4ããã³autoconfãŠãŒãã£ãªãã£ãå¿ èŠã§ããããšã«æ³šæããŠãã ããã
ããããã¹ãŠã®ã³ãã³ããå®è¡ãããšã ext / hello / modules /ãã©ã«ããŒã«hello.soãã¡ã€ã«ã衚瀺ãããŸãã ããã§ãä»ã®PHPæ¡åŒµæ©èœãšåæ§ã«ãåã«ãã®ãã£ã¬ã¯ããªãæ¡åŒµæ©èœã§ã³ããŒã§ããŸãïŒããã©ã«ãã§ã¯/ usr / local / lib / php / extensions /ã§ããã 念ã®ããã php.iniã®èšå®ã確èªãããã php-configã³ãã³ãã䜿çšããŸã- -extension-dir ïŒãããŠã php.iniã«extension = hello.soè¡ãè¿œå ããŠãPHPã®èµ·åæã«æ¡åŒµæ©èœã®ããŒããåæåããŸãã SAPIã¿ã€ãã®CGI / CLIã®å Žåãããã¯æ¬¡ã«å®è¡ãããã¹ã¯ãªããã«ãªããSAPIã¿ã€ãã®Apache WebãµãŒããŒã®å Žåãããã¯æ¬¡ã®ãµãŒããŒã®åèµ·åã«ãªããŸãã ã³ãã³ãã©ã€ã³ããé¢æ°ãåŒã³åºããŠã¿ãŸãããã
$ php âr 'echo hello_world();'
ãã¹ãŠãæ£åžžã«å®è¡ããããšã hello_worldé¢æ°ãããŒããããæ¡åŒµæ©èœããè¡ãè¿ããããã¹ã¯ãªããã®çµæãšããŠHello Worldè¡ã衚瀺ããã echoã³ãã³ãã¯éä¿¡ããããã®ã衚瀺ããŸãïŒãã®å Žåã hello_worldé¢æ°ã®çµæïŒ
ãã®äŸã§ã¯ãæååãè¿ããŸããããä»ã®ã¹ã«ã©ãŒããŒã¿åãåæ§ã®ååã«åŸã£ãŠè¿ãããšãã§ããŸãïŒæŽæ°å€ã®å Žåã¯RETURN_LONG ãæµ®åå°æ°ç¹æ°ã®å Žåã¯RETURN_DOUBLE ãTRUE / FALSEã®å Žåã¯RETURN_BOOL ãNULLå€ã®å Žåã¯RETURN_NULLã§ãã PHP_FEãã¯ããå«ãè¡ãfunction_entityæ§é ã«è¿œå ãã hello.cãã¡ã€ã«å ã®å¯Ÿå¿ããPHP_FUNCTIONãã¯ããè¿œå ããŠãããããã®åäœãèŠãŠã¿ãŸãããã
static function_entry hello_functions[] = {
PHP_FE(hello_world, NULL)
PHP_FE(hello_long, NULL)
PHP_FE(hello_double, NULL)
PHP_FE(hello_bool, NULL)
PHP_FE(hello_null, NULL)
{NULL, NULL, NULL}
};
PHP_FUNCTION(hello_long)
{
RETURN_LONG(42);
}
PHP_FUNCTION(hello_double)
{
RETURN_DOUBLE(3.1415926535);
}
PHP_FUNCTION(hello_bool)
{
RETURN_BOOL(1);
}
PHP_FUNCTION(hello_null)
{
RETURN_NULL();
}
ããã«ããã«ãããã»ã¹ãã¹ã ãŒãºã«é²ãããã«ã php_hello.hããããŒãã¡ã€ã«ã®hello_worldé¢æ°ã®ãããã¿ã€ãã®æšªã«ãããã®é¢æ°ã®ãããã¿ã€ããè¿œå ããå¿ èŠããããŸãã
PHP_FUNCTION(hello_world);
PHP_FUNCTION(hello_long);
PHP_FUNCTION(hello_double);
PHP_FUNCTION(hello_bool);
PHP_FUNCTION(hello_null);
config.m4ãã¡ã€ã«ã«å€æŽãå ããŠããªããããæè¡çã«ã¯phpizeãš./configureã®æé ãç¹°ãè¿ãå¿ èŠã¯ãããŸããããããã«makeã³ãã³ãã®å®è¡ã«é²ãããšãã§ããŸãã ãã ããä»åã¯ãåé¡ããªãããšã確èªããããã«ã3ã€ãã¹ãŠã®ã¢ã»ã³ããªæé ãå床å®è¡ããããã«ãé¡ãããŸãã ããã«ãæåŸã®æé ãšããŠã makeã®ä»£ããã«make clean all ã³ãã³ãã䜿çšããŠããã¹ãŠã®ãœãŒã¹ãã¡ã€ã«ãåæ§ç¯ãããããã«ããããšãã§ããŸãã ç¹°ãè¿ããããããã¯ããªããè¡ã£ãå€æŽã®çš®é¡ã®ããã«å¿ èŠã§ã¯ãªããããšã©ãŒã«ééãããããå®å šã«ãã¬ã€ããæ¹ãè¯ãã ã¢ãžã¥ãŒã«ãã¢ã»ã³ãã«ããããããããæ¡åŒµãã£ã¬ã¯ããªã«ã³ããŒããŠãå€ãããŒãžã§ã³ã«çœ®ãæããã ãã§ãã
ããã§ãPHPã€ã³ã¿ãŒããªã¿ãŒãå床åŒã³åºããŠãè¿œå ããæ©èœããã¹ãããããã«åçŽãªã¹ã¯ãªãããæž¡ãããšãã§ããŸãã å®éã«-ä»ãããã£ãŠã¿ãŸãããïŒ ããã§ããªããåŸ ã£ãŠããŸã...
çµãã£ãïŒ ããã echoã§ã¯ãªãvar_dumpã䜿çšããŠåé¢æ°ã®çµæã衚瀺ããå Žåã hello_boolãTRUEãè¿ãããšã«æ°ã¥ãããããããŸããã ããã¯ãé¢æ°ã®åŒæ°ã®1ã€ã®åŒæ°ãçããçµæã§ãã PHPã¹ã¯ãªããã®å Žåã®ããã«ãæŽæ°å€0ã¯FALSEãšåçã§ããããã®ä»ã®æ°å€ã¯TRUEãšåçã§ãã æ¡åŒµæ©èœã®äœæè ã¯ãå€ãã®å Žåããã®çªå·ã1ã§ããå¥çŽã䜿çšããŸãããã ãããã®å¥çŽãé å®ããããšãæãŸããã§ãããå¿ é ã§ã¯ãããŸããã ããã«ã RETURN_TRUEããã³RETURN_FALSEãã¯ãã¯ãã䟿å©ã«äœ¿çšã§ããŸãã 以äžã¯ã RETURN_TRUEãã¯ãã䜿çšããhello_boolé¢æ°ã®äŸã§ã ã
PHP_FUNCTION(hello_bool)
{
RETURN_TRUE;
}
ãã¯ããåŒã³åºããšãã«æ¬åŒ§ã䜿çšãããŠããªãããšã«æ³šæããŠãã ããã ãã®ç¹ã§ããã¯ãRETURN_TRUEãšRETURN_FALSEã¯ã RETURN_ *ãã¡ããªãŒã®æ®ãã®ãã¯ããšã¯ç°ãªãããã泚æããŠãã ããã
åã®ãã¹ãŠã®äŸã§ãçµæã®ã³ããŒãäœæãããã©ã¡ãŒã¿ãŒãæž¡ããªãããšã«æ°ã¥ãããããããŸããã ãã®çç±ã¯ããã®ãããªåçŽãªã¹ã«ã©ãŒéã®å Žåãè¿œå ã®ã¡ã¢ãªã¯å¿ èŠãªããã解æŸãããªãããã§ãã
ããã«3ã€ã®æ»ãã¿ã€ãããããŸããRESOURCE ïŒããšãã°ã mysql_connect ã fsockopenãŸãã¯ftp_connectã«ãã£ãŠè¿ãããŸãïŒã ARRAY ïŒå¥åHASH ïŒãããã³OBJECT ïŒ æ°ããããŒã¯ãŒãã«ãã£ãŠè¿ãããŸãïŒã ãããã«ã€ããŠã¯åŸã§èª¬æããŸãã
Inièšå®
Zend Engineã¯ãINIããŒã¿ãæäœããããã®2ã€ã®ã¢ãããŒããæäŸããŸãã ããã§ãæãåçŽãªãã®ãæ€èšããã°ããŒãã«å€æ°ã«æ £ããŠãããããäžè¬çãªãã®ã«æ»ããŸãã
æ¡åŒµçšã®php.iniãã¡ã€ã«ã§hello.greetingèšå®ã宣èšãããšããŸããããã«ã¯ã hello_worldé¢æ°ã«ããåºåã®å€ãå«ãŸããŸãã ãããè¡ãã«ã¯ã hello_module_entryæ§é å€æŽã®äžéšãšããŠã hello.cããã³php_hello.hãã¡ã€ã«ã«ããã€ãã®å€æŽãè¿œå ããå¿ èŠããããŸã ã php_hello.hãã¡ã€ã«ã®ãŠãŒã¶ãŒé¢æ°ãããã¿ã€ãã®ãªã¹ãã®åã«ã次ã®ãããã¿ã€ããè¿œå ããããšããå§ããŸãã
PHP_MINIT_FUNCTION(hello);
PHP_MSHUTDOWN_FUNCTION(hello);
PHP_FUNCTION(hello_world);
PHP_FUNCTION(hello_long);
PHP_FUNCTION(hello_double);
PHP_FUNCTION(hello_bool);
PHP_FUNCTION(hello_null);
次ã«ã hello.cãã¡ã€ã«ãéãã hello_module_entryã®çŸåšã®ããŒãžã§ã³ã次ã®ãã®ã«çœ®ãæããŸãã
zend_module_entry hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_HELLO_WORLD_EXTNAME,
hello_functions,
PHP_MINIT(hello),
PHP_MSHUTDOWN(hello),
NULL,
NULL,
NULL,
#if ZEND_MODULE_API_NO >= 20010901
PHP_HELLO_WORLD_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
PHP_INI_BEGIN()
PHP_INI_ENTRY( "hello.greeting" , "Hello World" , PHP_INI_ALL, NULL)
PHP_INI_END()
PHP_MINIT_FUNCTION(hello)
{
REGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(hello)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
ããã§ã hello.cãã¡ã€ã«ã®å é ã«é©åãª#includeãã£ã¬ã¯ãã£ããè¿œå ããŠãINIãã¡ã€ã«ãæäœããããã®ããããŒãååŸããå¿ èŠããããŸãã
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "php_hello.h"
æåŸã«è¡ãå¿ èŠãããã®ã¯ã hello_worldé¢æ°ãå€æŽããŠãINIèšå®ã䜿çšããããã«ããããšã§ãã
PHP_FUNCTION(hello_world)
{
RETURN_STRING(INI_STR( "hello.greeting" ), 1);
}
INI_STRããååŸããå€ã®ã³ããŒãäœæããŠããããšã«æ³šæããŠãã ããã ãã®çç±ã¯ã INI_STRã«ãã£ãŠè¿ãããæååã¯éçã§ããããã§ãã ã€ãŸããå€æŽããããšãããšãPHPç°å¢ãäžå®å®ã«ãªã£ãããã¯ã©ãã·ã¥ãããããå¯èœæ§ããããŸãã
ãã®ç« ã§è¡ãããå€æŽã®æåã®ã»ããã¯ã MINITããã³MSHUTDOWNãšãã2ã€ã®æ°ããã¡ãœãããæ¢ã«ç¥ã£ãŠããã¯ãã§ã ã åè¿°ã®ããã«ããããã®ã¡ãœããã¯ãSAPIã¬ã€ã€ãŒã®æåã®èµ·åæãšããã®äœæ¥ãå®äºããéçšã§ããããåŒã³åºãããŸãã ãªã¯ãšã¹ãäžãŸãã¯ãªã¯ãšã¹ãéã§ã¯åŒã³åºãããŸããã ãã®äŸã§ã¯ãæ¡åŒµæ©èœã§å®£èšãããphp.iniãã¡ã€ã«ãããšã³ããªãç»é²ããããã«äœ¿çšãããŸãã ãããã®é¢æ°ãªãœãŒã¹ã«ãããªããžã§ã¯ããšã¹ããªãŒã ãã³ãã©ãç»é²ã§ããŸãã
hello_worldé¢æ°ã§ã¯ã INI_STRãã¯ãã䜿çšããŠã hello.greatingãšã³ããªã®çŸåšã®å€ãæååãšããŠååŸããŸãã 以äžã«ãªã¹ããããŠããæŽæ°å€ãæµ®åå°æ°ç¹å€ãããŒã«å€ãååŸããããã®é¢æ°ã¯ä»ã«ããããããããŸãã ããã«ããããã®é¢æ°ã«ã¯ãæ¡åŒµåãORIGã®éè€ããããŸããããã«ããã php.iniãã¡ã€ã«ã«æžã蟌ãŸãã圢åŒã§ã¬ã³ãŒãã®å€ãååŸã§ããŸã ïŒ .htaccessãã¡ã€ã«ãŸãã¯ini_seté¢æ°ã䜿çšããŠå€æŽããåïŒã
çŸåšã®å€ | å ã®å€ | çš®é¡ |
---|---|---|
INI_STRïŒååïŒ | INI_ORIG_STRïŒååïŒ | Char *ïŒNULLçµäºïŒ |
INI_INTïŒååïŒ | INI_ORIG_INTïŒååïŒ | é·ã眲å |
INI_FLTïŒååïŒ | INI_ORIG_FLTïŒååïŒ | äºé眲å |
INI_BOOLïŒååïŒ | INI_ORIG_BOOLïŒååïŒ | Zend_bool |
PHP_INI_ENTRYé¢æ°ã«ç§»ããŸãããã æåã®ãã©ã¡ãŒã¿ãŒã¯ãé¢å¿ã®ããphp.iniãã¡ã€ã«ã®ãšã³ããªã®ååãå«ãæååã§ãã php.iniã®ãšã³ããªåã®è¡çªãé¿ããããã«ãé¢æ°ã«ååãä»ãããšããšåãèŠåã䜿çšããå¿ èŠããããŸããååã¯ãæ¡åŒµåã®ååãšäžèŽãããã¬ãã£ãã¯ã¹ã§å§ãŸãå¿ èŠããããŸãã ãŸããå¥çŽã§ã¯ãããªãªãã¯.iniãã¡ã€ã«ã®å ã®æ§æåããæ¡åŒµååãåé¢ããå¿ èŠããããšèŠå®ãããŠããŸãã ãã®å Žåãèšå®åã¯hello.greetingã®ããã«ãªããŸãã
2çªç®ã®ãã©ã¡ãŒã¿ãŒã¯åæå€ã§ãå€ãæ°å€ã§ãããã©ããã«é¢ä¿ãªããåžžã«char *ã«èšå®ãããŸãã ããã¯ããã¡ã€ã«èªäœãããã¹ãã§ãããããINIãã¡ã€ã«ã®ãã¹ãŠã®èšå®ãæ¬è³ªçã«ããã¹ãã§ãããšããäºå®ã®çµæã§ãã ã¹ã¯ãªããå ã§INI_INT ã INI_FLTããŸãã¯INI_BOOLãã¯ããåŸã§äœ¿çšãããšããããã®ã¿ã€ããå€æãããŸãã
3çªç®ã®ãã©ã¡ãŒã¿ãŒã¯ãã¢ã¯ã»ã¹ã¬ãã«ä¿®é£Ÿåã§ãã ããã¯ãç¹å®ã®INIèšå®ããã€ã©ã®ããã«å€æŽã§ãããã決å®ããããããã¹ã¯ã§ãã register_globalsãªã©ã®äžéšã®èšå®ã§ã¯ãã¹ã¯ãªããã解決ããæ©äŒãäžããããåã®ãªã¯ãšã¹ãåŠçã®æºåäžã«ã®ã¿æå³ãããããã ini_seté¢æ°ã䜿çšããŠã¹ã¯ãªããå ã§å€ãå€æŽããããšã¯èš±å¯ãããŸããã allow_url_fopenãªã©ã®ãã®ä»ã®èšå®ã¯ããŠãŒã¶ãŒãini_setãŸãã¯.htaccessãã£ã¬ã¯ãã£ãã䜿çšããŠå€æŽããããšãèš±å¯ããªã管çèšå®ã§ã ã ãã®ãã©ã¡ãŒã¿ãŒã®ããã©ã«ãå€ã¯PHP_INI_ALLå€ã§ãèšå®å€ã¯ã©ãã§ãå€æŽã§ããããšã瀺ããŸãã PHP_INI_SYSTEM | PHP_INI_PERDIRã®å€ãå¯èœã§ããããã¯ãèšå®å€ãphp.iniãŸãã¯.htaccessãã¡ã€ã«å ã®ãã£ã¬ã¯ãã£ããä»ããŠãini_setïŒïŒé¢æ°ãä»ããŠå€æŽã§ããããšã瀺ããŸãã ãŸãã¯ã PHP_INI_SYSTEMå€ãå¯èœã§ããã€ãŸããèšå®ã¯php.iniãã¡ã€ã«ã§ã®ã¿å€æŽã§ã ãä»ã®å Žæã§ã¯å€æŽã§ããŸããã
æåŸã®4çªç®ã®ãã©ã¡ãŒã¿ãŒã§ã¯ã ini_seté¢æ°ã䜿çšããŠèšå®ãå€æŽããããšãã«åŒã³åºãããã³ãŒã«ããã¯é¢æ°ãæå®ã§ããŸãã ããã«ãããæ¡åŒµæ©èœã¯ãèšå®ãå€æŽããæ¡ä»¶ãããå®å šã«å¶åŸ¡ããããæ°ããèšå®å€ã«å¿ããŠå¯Ÿå¿ããæ©èœãåŒã³åºãããšãã§ããŸãã
ã°ããŒãã«å€æ°
å€ãã®å Žåãæ¡åŒµæ©èœã¯å¥ã®ãªã¯ãšã¹ãã§å€æ°ãåŠçããå¿ èŠãããããã®å€ãåæã«åŠçã§ããä»ã®ãªã¯ãšã¹ãããç¬ç«ããŠããŸãã ãã«ãã¹ã¬ããSAPIã§ã¯ãããã¯éåžžã«ç°¡åã«è¡ããŸãããœãŒã¹ãã¡ã€ã«ã§ã°ããŒãã«å€æ°ã宣èšããå¿ èŠãªãšãã«åç §ããã ãã§ãã åé¡ã¯ãPHPã¯ãã«ãã¹ã¬ããWebãµãŒããŒïŒApache 2ãISSãªã©ïŒã§åäœããããã«èšèšãããŠãããããäžæ¹ã®ã¹ã¬ããã§äœ¿çšãããã°ããŒãã«å€æ°ãä»æ¹ã®ã°ããŒãã«å€æ°ãšã¯å¥ã«ä¿åããå¿ èŠãããããšã§ãã PHPã¯ã ZTS ïŒZend Thread SafetyïŒãšåŒã°ããããšãããTSRM ïŒThread Safe Resource ManagerïŒæœè±¡åã¬ã€ã€ãŒã䜿çšããããšã«ããããã®ã¿ã¹ã¯ãå€§å¹ ã«ç°¡çŽ åããŸãã å®éããã®èšäºã§ã¯TSRMã®äžéšãæ¢ã«äœ¿çšãããŠããŸãããæ°ã¥ããŠããŸããã§ããã ïŒä»ã®ãšããããã¯ããªãã«ãšã£ãŠé£ããããã®ã§ãããããèŠã€ããããšããªãã§ãã ããïŒã
ã¹ã¬ããã»ãŒãã°ããŒãã«å€æ°ãäœæããæåã®éšåã¯ãä»ã®ã°ããŒãã«å€æ°ãšåæ§ã«ã宣èšããããšã§ãã äŸãšããŠãåæå€ã0ã«ãªãlongåã®ã°ããŒãã«å€æ°ã1ã€å®£èšããŸããhello_longé¢æ°ãåŒã³åºããããã³ã«ãã°ããŒãã«å€æ°ã®å€ãå¢ãããŠãã®å€ãè¿ããŸãã
#define PHP_HELLO_Hè¡ã®çŽåŸã«ã次ã®ã³ãŒãã¹ãããããphp_hello.hã«è¿œå ããŸãã
#ifdef ZTS
#include "TSRM.h"
#endif
ZEND_BEGIN_MODULE_GLOBALS(hello)
long counter;
ZEND_END_MODULE_GLOBALS(hello)
#ifdef ZTS
#define HELLO_G(v) TSRMG(hello_globals_id, zend_hello_globals *, v)
#else
#define HELLO_G(v) (hello_globals.v)
#endif
ãŸãã RINITã¡ãœããã䜿çšããå¿ èŠããããããããããŒã§ãããã¿ã€ãã宣èšããå¿ èŠããããŸãã
PHP_MINIT_FUNCTION(hello);
PHP_MSHUTDOWN_FUNCTION(hello);
PHP_RINIT_FUNCTION(hello);
hello.cãã¡ã€ã«ã«ç§»åããŠãã€ã³ã¯ã«ãŒããããã¯ã®åŸã«æ¬¡ã®ã³ãŒããè¿œå ããŸãããã
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "php_hello.h"
ZEND_DECLARE_MODULE_GLOBALS(hello)
PHP_RINITïŒhelloïŒãè¿œå ããŠhello_module_entryãå€æŽããŸã ã
zend_module_entry hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_HELLO_WORLD_EXTNAME,
hello_functions,
PHP_MINIT(hello),
PHP_MSHUTDOWN(hello),
PHP_RINIT(hello),
NULL,
NULL,
#if ZEND_MODULE_API_NO >= 20010901
PHP_HELLO_WORLD_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
ãããŠããªã¯ãšã¹ãã®éå§æã«åæåãåŠçããé¢æ°ãããã€ãè¿œå ãããšãšãã«ã MINITé¢æ°ãå€æŽããŸãã
static void php_hello_init_globals(zend_hello_globals *hello_globals)
{
}
PHP_RINIT_FUNCTION(hello)
{
HELLO_G(counter) = 0;
return SUCCESS;
}
PHP_MINIT_FUNCTION(hello)
{
ZEND_INIT_MODULE_GLOBALS(hello, php_hello_init_globals, NULL);
REGISTER_INI_ENTRIES();
return SUCCESS;
}
æåŸã«ãã°ããŒãã«å€æ°ãè¿œå ããŠhello_longé¢æ°ãå€æŽã§ããŸãã
PHP_FUNCTION(hello_long)
{
HELLO_G(counter)++;
RETURN_LONG(HELLO_G(counter));
}
php_hello.hãã¡ã€ã«ã«å ããå€æŽã§ã¯ãããã€ãã®ãã¯ã-ZEND_BEGIN_MODULE_GLOBALSãšZEND_END_MODULE_GLOBALSã䜿çšããŸããã 圌ãã®å©ãã«ããã zend_hello_globalsæ§é äœãå®çŸ©ãããlongåã®å€æ°ã1ã€å«ãŸããŠããŸãã ãã®åŸã HELLO_Gãã¯ããå®çŸ©ãããŸãããããã«ãããã³ã³ãã€ã«ã¢ãŒãã«å¿ããŠïŒãã«ãã¹ã¬ããã®æç¡ã«ãããããïŒã¹ã¬ããããŒã«ããå€ãååŸããããã°ããŒãã«ã¹ã³ãŒãããå€ãååŸãããã§ããŸãã
hello.cãã¡ã€ã«ã§ã ZEND_DECLARE_MODULE_GLBALSãã¯ãã䜿çšããŠã zend_hello_globalsæ§é äœãã°ããŒãã«ïŒéã¹ã¬ããã»ãŒããã«ãã®å ŽåïŒãŸãã¯ã¹ããªãŒã ãªãœãŒã¹ããŒã«ã®ã¡ã³ããŒãšããŠå®£èšããŸããã æ¡åŒµæ©èœã®äœæè ã§ããç§ãã¡ã¯ãZEããã¹ãŠã®äœæ¥ãåŠçããããããã®ã¡ã«ããºã ã«ã€ããŠèããå¿ èŠã¯ãããŸããã æåŸã«ã ZEND_INIT_MODULE_GLOBALSé¢æ°ã䜿çšããŠãã¹ã¬ããã»ãŒããªãªãœãŒã¹èå¥åãå²ãåœãŠãŸãã
ãæ°ä»ããããããŸãããã php_hello_init_globalsé¢æ°ã¯å®éã«ã¯äœãããŸããã ããã«ãã«ãŠã³ã¿ãŒã0ã«åæåãããšã RINITé¢æ°ã«ããããšãå€æããŸããã ãªãã§ïŒ
ãã®è³ªåã«çããéµã¯ããããã®é¢æ°ãåŒã³åºãããç¬éã«ãããŸãã php_hello_init_globalsé¢æ°ã¯ ãæ°ããããã»ã¹ãŸãã¯ã¹ã¬ãããéå§ããããšãã«ã®ã¿åŒã³åºãããŸãã ãã ããåããã»ã¹ã¯è€æ°ã®èŠæ±ãåŠçã§ããããããã®é¢æ°ã䜿çšããŠã«ãŠã³ã¿ãŒã0ã«åæåããããšã¯ãæåã®èŠæ±ã«å¯ŸããŠã®ã¿trueã«ãªããŸãã åãããã»ã¹ãžã®åŸç¶ã®ãªã¯ãšã¹ãã¯ãå€ãã«ãŠã³ã¿å€ã§åŒãç¶ãæ©èœãããããæåããã¬ããŒããéå§ããããšã¯ãããŸããã ãªã¯ãšã¹ãããšã«ã«ãŠã³ã¿ã0ã«åæåããããã«ã RINITé¢æ°ã䜿çšããŸãããããã¯ãæ¢ã«èªãã ããã«ãåã¹ã¯ãªããåŠçãªã¯ãšã¹ãã®åã«åŒã³åºãããŸãã å°ãªããšãã察å¿ãããã©ã¡ãŒã¿ãŒZEND_INIT_MODULE_GLOBALSãšããŠNULLãinité¢æ°ã«æž¡ããšãã¹ã¬ãããµããŒãã®ãªããã©ãããã©ãŒã ã§ã¯ã»ã°ã¡ã³ããŒã·ã§ã³ãšã©ãŒãçºçããããã php_hello_init_globalsé¢æ°ãã³ãŒãã«å«ããŸããã
INIèšå®ãšã°ããŒãã«å€æ°
ãã§ã«ãåç¥ã®ããã«ã PHP_INI_ENTRYé¢æ°ã䜿çšããŠå®£èšãããphp.iniãã¡ã€ã«ã®èšå®ã¯æååãšããŠèªã¿åãããå¿ èŠã«å¿ããŠINI_INT ã INI_FLTããã³INI_BOOLé¢æ°ã䜿çšããŠä»ã®åœ¢åŒã«å€æãããŸã ã ãã ããã¹ã¯ãªããã®å®è¡äžã«ããã€ãã®èšå®ãé »ç¹ã«èŠæ±ããããã¡ã€ã«ããã®äžèŠãªèªã¿åãæäœã倧éã«çºçããå¯èœæ§ããããŸãã 幞ããªããšã«ãç¹å®ã®ããŒã¿åã«INIèšå®ãä¿åããèšå®å€ãå€æŽããããšãã«ã®ã¿åå€æãå®è¡ããããã«ZEã«æ瀺ããããšãã§ããŸãã äœæ¥äžã«ãã®å¯èœæ§ã確èªããããã«ããã1ã€ã®INIèšå®ã宣èšããŸããä»åã¯ããŒã«åã§ãçŸæç¹ã§ã«ãŠã³ã¿ãŒããã¯ãªã¡ã³ããããå¢ãããã決å®ããŸãã p hp_hello.hã® MODULE_GLOBALSãããã¯ã次ã®ããã«å€æŽããããšããå§ããŸãã
ZEND_BEGIN_MODULE_GLOBALS(hello)
long counter;
zend_bool direction;
ZEND_END_MODULE_GLOBALS(hello)
次ã®ã¹ãããã¯ã PHP_INI_BEGINãããã¯ãå€æŽããŠINIèšå®ã宣èšããããšã§ãã
PHP_INI_BEGIN()
PHP_INI_ENTRY( "hello.greeting" , "Hello World" , PHP_INI_ALL, NULL)
STD_PHP_INI_ENTRY( "hello.direction" , "1" , PHP_INI_ALL, OnUpdateBool, direction, zend_hello_globals, hello_globals)
PHP_INI_END()
ããã§ã init_globalsé¢æ°ã®èšå®ãåæåããŸãã
static void php_hello_init_globals(zend_hello_globals *hello_globals)
{
hello_globals->direction = 1;
}
æåŸã«ã hello_longé¢æ°ã®INIèšå®ã®å€ã䜿çšããŠãã«ãŠã³ã¿ãŒã®æäœãéžæããŸãã
PHP_FUNCTION(hello_long)
{
if (HELLO_G(direction)) {
HELLO_G(counter)++;
} else {
HELLO_G(counter)--;
}
RETURN_LONG(HELLO_G(counter));
}
以äžã§ãïŒ STD_PHP_INI_ENTRYãã¯ãã®3çªç®ã®ãã©ã¡ãŒã¿ãŒãšããŠæž¡ãããOnUpdateBoolã¡ãœããïŒã¡ãœããã¯ZEã®äžéšïŒã䜿çšããŠã php.ini ã .htaccessãã¡ã€ã«ããååŸãããèšå®å€ã®ã¿ã€ããã察å¿ããTRUE / FALSEå€ã«ini_seté¢æ°ã䜿çšããŠèªåçã«å€æãããŸãããããã¹ã¯ãªããå ã§çŽæ¥ååŸã§ããŸãã STD_PHP_INI_ENTRYé¢æ°ã®æåŸã®3ã€ã®ãã©ã¡ãŒã¿ãŒã¯ãå€æŽããã°ããŒãã«å€æ°ãæ¡åŒµæ©èœã®ã°ããŒãã«å€æ°ã®æ§é ãããã³ããããå«ãŸããã°ããŒãã«å€æ°ã®ã³ã³ãããŒã®ååãPHPã«äŒããŸãã
次ã¯ïŒ
ãã®èšäºã§ã¯ãå€ãè¿ãé¢æ°ããšã¯ã¹ããŒãããINIèšå®ãããŒãããŠåŠçãããªã¯ãšã¹ãã®åŠçå šäœã§ããããä¿æããåçŽãªPHPæ¡åŒµã®æ§é ã調ã¹ãŸããã
次ã®èšäºã§ã¯ãPHPå€æ°ã®å éšæ§é ããããã®ã¹ãã¬ãŒãžãšåŠçæ¹æ³ã«ã€ããŠèŠãŠãããŸãã é¢æ°ãåŒã³åºããããšãã«ããã°ã©ã ãããã©ã¡ãŒã¿ãŒãååŸããããã«äœ¿çšãããzend_parse_parametersé¢æ°ã䜿çšããé åããªããžã§ã¯ãããªãœãŒã¹ãªã©ãé¢æ°ããããè€éãªçµæãè¿ãæ¹æ³ãéããŸãã
PS
äžéšã®å Žæã§ã¯ã翻蚳ã¯ããªãèªç±ãªåœ¢åŒã«ãªã£ãŠããŸãããããã¯ç¿»èš³ã®é£ããã®çµæã§ãããç§ã¯ãããå®å šã«å æããããšã¯ã§ããŸããã§ãããããã«ã翻蚳ã«ã¯ãç§ã«é¢ä¿ããããšæãããå°ããªè¿œå ãããã€ãå«ãŸããŠããŸãã
ãã®èšäºã«ããæ¡åŒµæ©èœã®ãã«ãããã»ã¹ã¯ãPHP 5.3.2ã®ãœãŒã¹ã³ãŒãã§ãã¹ããããŸããã
ã§èè ïŒãã®ããŒãã«é¢ããããã€ãã®èšäºããã1ã2.1ã2.2ã3ã4çªç®ã®èšäºã¯ãæããã«ããã®ããŒãã«çŠç¹ãåœãŠããPHPã®æ¡åŒµãšåã蟌ã¿ãã®èè ã«ããåºçç©ãèæ ®ããŠã決ããŠå ãèŠãããšã¯ãªãã§ããããphp.netã§
Zend Engineã«ã€ããŠèªãããšãã§ããŸãã