vµÔèÌXVÅ·BÅßACs¾ÆvÁÄFXÈ{ðÇÝÁÄ¢éÌÅ·ªAâÁƱ³PHPÌinterfaceªª©éæ¤ÉÈÁÄ«½´¶ª éÌÅAÜÆßÄÝÜ·B
interfaceÆÍ
C^[tF[XÍANX©ç"[U[è`^"Ì\ÍÌÝ𪣵½¾ê@\Å·BPHPÌ»êÍJavaÌinterfaceÌpNÅ·B
"[U[è`^"Æ¢¤Pêðg¢Üµ½B®I^t«¾ê̱Æðu^ÌÈ¢¾êvƾ¤lª½ÜÉ¢éñÅ·ªAÆñÅàË[Ôá¢Å·BPHPɾÁÄ^Í èÜ·BϪ^ð½¸Alª^ðÁÄ¢éÆ¢¤¾¯ÌbÅ·B
interfaceðé¾·é¶@ÍNXÆæĢܷªAL[[hclassÌãíèÉL[[hinterfaceðg¢Ü·BgÍè`ÌÈ¢\bhÌé¾ð«Ü·BȨAèàÜßé±ÆªÂ\Å·B
<?php
interface FooInterface
{
const MOGE = 1;
public function doSomething1();
public function doSomething2(DateTime $date);
}
Å« ªÁ½FooInterfaceÍANXÅÍÈ¢ÌÅnewÅCX^X»·é±ÆÍūܹñBimplementsL[[hðg¢ANXƵÄgðÀ·éÆnewÅ«éæ¤ÉÈèÜ·B
//\[XR[hͳÁ«Ìà̩籢ĢéC[WÅ·
class Foo implements FooInterface
{
public function doSomething1()
{
}
public function doSomething2(DateTime $date)
{
}
}
$foo = new Foo;
var_dump($foo instanceof FooInterface); //true
interfaceͽÌðɧÂÌ©
ÍÅAinterfaceÌg¢¹ªæí©èܹñŵ½BCX^X»Å«È¢NXÌo¹È¢ÅAé¾·êÎLqʪ¦é¾¯Å·BÀÛAinterfaceðêØgí¸ÆàPHPÌR[hܷͯBcÜ A»ñȱÆð¾¢nßéÆAgoto¶³¦ êÎfor¶àtry catchàÖàsvAÁıÆÉÈÁĵܢܷBinterfaceª¶Ý·éÌÍÖÈêʪ é©çÅ·B
interfaceÍ»êPÆÅÍðɧ¿Ü¹ñBàÁÏçA^CvqeBOÆgÝí¹Äg¢Ü·B^CvqeBOÉgíêĢȢinterfaceª\[XR[hÉo»µ½çAu¨Oͽªµ½¢ñ¾vÆr [RgÉ¢ÄOKÅ·B(³mÉͼÉàg¢¹ª éÌÅ·ªAââ±µÈéÌÅêU»¤¢¤±ÆɵĨ¢Ä¾³¢)
//...
function useFoo(FooInterface $foo) {
$foo->doSomething1();
$foo->doSomething2(new DateTime);
}
$foo instanceof FooInterface ð½µÄ¢È¢$fooðnµ½êA±ÌuseFoo()ÖðÀs·éÆG[ª¶µÄPHPªâ~µÜ·B®I¸ÈÌÅøÊÍÀèIÅ·ªAJobW100%ÌjbgeXgÆgÝí¹êÎúÉvOÌâèÉCñƪūéŵå¤B
±ÌêA^CvqeBOðÊßµ½$fooÍA\bhdoSomething1ÆdoSomething2ðÁÄ¢é±ÆÆÆA\bhÌøÌ^ªÛسêĢܷB
NXÉæé^CvqeBOÆÌá¢
ƱëÅA^CvqeBOÉÍNXàg¦Ü·B
function useFoo2(Foo $foo) {
$foo->doSomething1();
$foo->doSomething2(new DateTime);
}
¶á AâÁÏèinterfaceÈñÄgí¸ÉANX¾¯gÁ½Ù¤ª¨èy¶áÈ¢©AÆv¤©àµêܹñB
±ÌÓèÍवÀáðo³È¢ÆsƱȢàÌÅ·BinterfaceªðɧÂÌÍAवå«È±xA¢Â©ÌNXðÜÆß½pbP[WÌæ¤ÈTOªoÄ«½Å·B
æ éáƵÄAWebAvP[Vt[[NÆAev[gGWðgÝí¹Äg¤±Æðl¦Ü·B
t[[NͽÅàæ¢Å·BSymfonyÅàZFÅàD«ÈàÌðzµÄ¾³¢BÈñ©í©çñªt[[NÆ¢¤à̪ éAƵܷB
åïÌt[[NÍev[gGWðgÁÄAev[gãÉÏðWJµÄAHTML𶬵ܷBµ©µ¢ÌÉÍSmartyâçTwigâçMustacheâçA½Ìev[gGWª éÌÅADÝɶķµÖ¦çêéæ¤Éµ½¢Å·B
ev[gGWàA±êܽ½©æí©èܹñªAuev[g̶ñÆÏÌgðn¹ÎHTMLÉÏ·µÄêéA¹ïÌæ¤ÈIuWFNgvƵܷBt[[Nªev[gGWðg¤ÖWÅ·B
³ÄAev[gGWÍ·µÖ¦çêéæ¤Éµ½¢Ìŵ½Bµ©µev[gGWªu½¾©í©çñª¹ïIÈIuWFNgvÆ¢¤¾¯ÅÍAdlªí©çÈ¢ÌÅg¢æ¤ª èܹñB»±ÅAdlðßÅ¿µÄg¤±ÆɵܷBàµAev[gGW¤ªßÅ¿µ½àÌÆá¤dlÈçÎA·ªðæèÂbp[IuWFNgðìê΢¢ÌÅ·B
±ÌÆ«At[[NÝvÒÍANXÅ^CvqeBO·é©AinterfaceÅ^CvqeBO·é©IԱƪūܷB
namespace MyFramework;
class DefaultTemplateEngine {
public function render($template, array $context)
{
//...
}
}
//...
class FrameworkController {
//...
public function renderView(DefaultTemplateEngine $templateEngine, $template, array $context)
{
return $templateEngine->render($template, $context);
}
//...
}
namespace MyFramework;
interface TemplateEngineInterface {
public function render($template, array $context);
}
class FrameworkController {
//...
public function renderView(TemplateEngineInterface $templateEngine, $template, array $context)
{
return $templateEngine->render($template, $context);
}
//...
}
cܾsƱȢŷËBÅÍæÙÇÌ}ÉAà¤êloêl¨ðâµÄÝܵå¤Bev[gGWÍ[Ìè^¶ðµ¤ÌÉàÖÈÌÅA[[௶ev[gGWðg¤ÆµÜ·B
±Ì[[àNXðv·é©AC^[tF[Xðv·é©I×Ü·B³Ä³ÄAÇÁ¿ªDêÄ¢éŵ天H
¼ÒªNXðvµ½ÆµÜ·B·éÆAìðev[gGWÉÚ÷·é¾¯ÌNXðpbP[W²ÆÉìçȢƢ¯Ü¹ñBÈñ©Ê|³¢Å·ËBÀàÊ|Å·µACX^Xðg¢ª¯È¯êÎÈçÈ¢ÌÅÇàÏGÉÈèÜ·B
//t[[NpÌev[gGWNX
class FrameworkTemplateEngine extends MyFramework\DefaultTemplateEngine
{
//...
}
//[[pÌev[gGWNX
class MailerTemplateEngine extends MyMailer\DefaultTemplateEngine
{
//...
}
¼ÒªC^[tF[Xðvµ½ÆµÜ·B·éÆAñÂÌC^[tF[XðÀµ½êÂÌNXðì龯ÅÏÝÜ·BNXªêÂÆ¢¤±ÆÍCX^XàêÂÅæ¢ÌÅACX^XÌÇàP¾õÅ·BîêNXÍev[gGW̤ÌNXðg¤±ÆªÅ«éÌÅAÀàÈPÉÈèÜ·B
//t[[Nà[[à¼ûg¢Üí¹éev[gGWNX
class TemplateEngine extends HogeTemplateEngine implements
MyMailer\ViewInterface,
MyFramework\TemplateEngineInterface
{
//...
}
PHPÌNXÍPêp³µ©³êĢܹñBNXÅ^CvqeBO·éÆ¢¤±ÆÍA»ÌNX©AàµÍh¶NXðv·éÆ¢¤±ÆÅ·B±êÍÀÍAñíɵµ¢§ñÅ·BC^[tF[XÌêÍPêp³Ì§ñðó¯È¢½ßA§ñªÉÈèÜ·B
pbP[WªNXðv·éÆAí´í´êpÉNXðìçÈÄÍÈçÈ¢µAµ©àìÁ½NXͼÌpbP[WÅg¢ÜíµÅ«ÈÈèÜ·BNXðv·éÆ¢¤ÌÍuIl̬VÉ]¦IIÄpHIÌmÁ½±ÆÅÍÈ¢Ivݽ¢Éã©çÚüÌÌ»¤ÈÝvÈÌÅ·BºiÅ·ËB
Æ¢¤í¯ÅA^CvqeBOÅNXÅÍÈC^[tF[Xðv·é±ÆÅAªÅgÝí¹â·¢CuªìêÄAµ©à^Éæéi éöxÌji¿ÛتūܷB³ÅÉNXðv·éÌÍsì@ÈÌÅâßܵå¤B
PHPÌinterfaceÌÊÚÈƱë
´ÁÆinterfaceÌg¢Ç±ëÉ¢Ģīܵ½ªAæl¦éÆPHPÌinterfaceÍC}C`ÈÆ±ëª èÜ·Bu®Iȸµ©Å«È¢vÆ¢¤Ì;êIÉdûÈ¢ÌÅ·ªA»êÈOÉàÊÚÈƱëªêtÅ·B
^ÌLqͪnã
interfaceð^ƾ¤ÉÍALqͪKvÅ·Bµ©µPHPÌ^VXeÍÆÄànãÅ·BinterfaceÉÍ\bhªÀÝ·é±ÆÆA\bhÌøÌ^µ©LqūܹñB\bhÌßèlÌ^ͦ¹È¢µA^ðøÉæéæ¤È^âAÖð¦·^àLqūܹñB
}WbN\bhÆÌ«ª«¢
PHPÌNXÉͶݵȢ\bh𠽩à¶Ý·éæ¤É©¹©¯é½ßÌ__callâ__getÆ¢Á½}WbN\bhª¶ÝµÜ·Bµ©µAinterfaceÉæé^CvqeBOÅÍA±êçÌgÜÅÍÛØÅ«È¢½ßAgÝí¹Äg¤±ÆªÅ«Ü¹ñB
}WbN\bhðÛص½¢êÍAfinalðKXgÝí¹½ãÅANXÅÌ^CvqeBOªK{ÉÈèÜ·B
½dÀªÛسêĢȢ
½æèv½IÈÌÍA±Ì_Å·BPHPÉÍ\bhÌI[o[[h@\ªÈ¢ÌÅAVOl`ª£·éÆÀÅ«ÈÈÁĵܤÌÅ·B¹Á©¾êIÉKvàÈ¢ÌÉinterfaceðè`µÄA¨V§ÄµÄâÁÄàg¦È¢±Æª é̾©ç½ÜçñÅ·B»ÀÉAPsr\Log\LoggerInterfaceÆAZend\Log\LoggerInterfaceÍVOl`ª£·é½ßA¯ÉÀūܹñB
ÀÅ«È¢P[XÍÈOÉQiitaÅÜÆß½ÌÅ»¿çðQƵľ³¢B
PHP - interfaceªG[ÉÈéP[X - Qiita [L[^]±ñÈÉcOÈƱ몽¢½ßAPHPɨ¢ÄÍA ÜèC^[tF[Xð·éÆg¢É¢\[XR[hÉÈéæ¤ÈCªµÜ·BÈÆ੪ŧäÅ«épbP[WàÌ^É¢ÄÍANXÅ^CvqgµÄàâèÈ¢Æv¢Ü·B
ÆÍ¢¦AÛNXðv·éæèÍinterfaceðvµ½ûª½dÀÌÂ\«ªc³êĢ龯ܾ}VÈÌÅApbP[WOÆÌ«EãÍAâÍèinterfaceðgÁÄLqµÄ¨¢½ûªeØÅ·B
PHPÌinterfaceÍöÌ^Å é
ƱëÅAPHPÌinterfaceÍ ÜÅé¾É^ªÜèAôR¯¶VOl`ðÁÄ¢Ä௶^¾ÆͩȳêܹñB±¤¢¤«¿ðöÌ^ÆÄÑÜ·B
<?php
interface FooInterface {
public function doSomething();
}
interface MooInterface {
public function doSomething();
}
class Hoge implements FooInterface {
public function doSomething() { }
}
$hoge = new Hoge;
var_dump($hoge instanceof FooInterface); // true
var_dump($hoge instanceof MooInterface); // false. ¯¶VOl`ðÁÄ¢é¯ÇÊÌ^
öÌ^ÆÍtÉAé¾Ì¼OÍ Üèdv³ê¸AôR¯¶VOl`ðÁÄ¢êί¶^ÆÝÈ·¾êà èÜ·B±Ì§êð\¢Iª^(Structural Subtyping)ƾ¢Ü·BPHPÉ\¢Iª^ÍÀ³êĢȢÌÅAcOȪçg¦Ü¹ñB\¢Iª^ªÀ³êÄ¢é¾êƵÄÍTypeScriptâScalaÈǪ èÜ·B
//TypeScriptÌá
class A { }
interface I { }
var a : A = new A;
var i : I = a; //³ÖWÌI^ÏÉãüµÄ¢éªAG[ÉÈçÈ¢B
\¢Iª^ÍÃIÈ_bN^CsOÅ éƾíêé±Æª èÜ·BöÌ^Í ÜÅé¾É¢Ä é±Æµ©MpµÈ¢ÌÅA¨ðIÆ¢¤©AZʪ©È¢C[WÅ·BÂlIÉÍA\¢Iª^Ìûªgð©ÄêéÌÅ_îÅÖÈCªµÄ¢Ü·B
öÌ^Ì_
µ©µAöÌ^¾©ç±»Ì·à èÜ·B
\¢Iª^ÌêAôRÌVOl`êvÈÌ©AÓ}µÄêv³¹½Ì©ªæÊūܹñBöÌ^ÌêAÈÆàé¾ÉwèµÄ¢éÆ¢¤±ÆÍAinterfaceÌdlðÇÝAÓ}µÄÀµÄ¢éÆ¢¤\¾ÉÈéÌÅ·B
±Ì«¿ÍAÐ骦¹Î^ÌLqͪnãÅàJo[Å«éÆ¢¤±ÆÅ·B¾ÁÄgæè\¾µÄ¢é±Æªå¾Æ¾¤ÌÅ·©çB¿åÁÆXs` AÈ´¶Å·B
»µÄ±ÌöÌ^Ì«¿ðÅåÀp·ép^[±»ª}[J[C^[tF[XÅ·B}[J[C^[tF[XÆÍAgªóÉÈÁÄ¢éC^[tF[X̱ÆÅ·B
interface Marker { }
t@bIH ½»êAC^[tF[XÌÓ¡È¢¶áñB¢â¢âAöÌ^Å êÎg¢¹ª éñÅ·B ÆÅڵྵܷB
æÙÇPHPÌCPÄȢƵÄA½dÀªÅ«È¢P[Xª éÆq×ܵ½ªA}[J[C^[tF[XÍgªÈ¢ÌÅ£ªâÎÉN±èܹñB}[J[C^[tF[XÅ êÎAPHPÅà½dÀªÛسêéÌÅAÀSµÄg¦Ü·B
}[J[C^[tF[Xðg¢±È·
}[J[C^[tF[XÌg¢¹ÆµÄÍAáOÌ_îÈߨâAAme[VÌãÖª°çêÜ·B
áOÌ_îÈߨ
áOÌߨÉÍ^CvqeBOðpÅ«Ü·B
SPLÉWIÈáOªêÊèÀ³êÄ¢éÌÅAÊíͱ¿çÌ^KwðgÁÄáOߨðLq·é͸ŷBµ©µÈªçAÆ©ÌáOðìÁÄÆ©ÌáOðµ½¢êà éŵå¤B±ÌAÆ©ÌáONXðè`µÄµÜ¤ÆASPLÆÍÊÌp³c[É®µÄµÜ¤½ßAêÊIÈSPLÌáOߨªg¦ÈÈÁĵܢܷB
±ñÈA}[J[C^[tF[XðgÁÄ^ð~bNXC·éÆASPLÅàÆ©^ÅàáOðߨūéæ¤ÉÈèÜ·B
interface MyExceptionMarker { }
class MyInvalidArgumentException extends InvalidArgumentException implements
MyExceptionMarker
{
}
class MyOutOfBoundsException extends OutOfBoundsException implements
MyExceptionMarker
{
}
//...
//±êÅàáOðߨūéµ
try {
doSomething();
} catch (InvalidArgumentException $e) {
//...
} catch (OutOfBoundsException $e) {
//...
}
//±êÅàáOðߨūé
try {
doSomething();
} catch (MyExceptionMarker $e) {
//...
}
±Ìp^[ÍZend FrameworkÈÇÅgíêĢܷB
Ame[VÌãÖ
}[J[C^[tF[XÍANXÉt^³ê½^îñÆ©È·±ÆàÅ«Ü·BNXÉ^OðÅÁÄ¢éæ¤È´¶Å·BÈÌÅANXAme[VÌãÖèiƵÄp·é±ÆªÅ«Ü·B
½Æ¦ÎDIReiÅÁèÌNXðCX^X»·éÛA é}[J[C^[tF[Xªt^³êÄ¢êÎvpeBðCWFNV·éÆ©ACX^XÌÇðVOgÉÏX·éÆ¢Á½±ÆªÂ\Å·B±±ÅÍïÌIÈÀͦµÜ¹ñªAuNX»ÌàÌðµ¤CuvÆgÝí¹éÆFXÈg¢ûª é͸ŷB
namespace My\MarkerInterface as at;
//NXÉuVOgƵĵ¦vuCW[[hÅÇÝßvÆ¢Á½óð¯Ģé
class Hoge implements
at\Singleton,
at\LazyLoad
{
//...
}
Ame[VÌCuÍdoctrine/commonÈǪ èÜ·ªA}[J[C^[tF[XÌêÍtNV·çgí¸AfÌPHPÌ@\¾¯ÅÀ»Å«é½ßACuÉä×ij|IɬɮìµÜ·BÈÌÅ}[J[C^[tF[XÌg¢Ç±ëÍFX¤Å«éÌÅÍȢŵ天B
ÜÆß
- CuÌvOC@\ªÅÁèÌNXðv·éÌÍã©çÚüÅJÂìèÅ·B»¤¢¤Ì»¤ÈCuÍìçÈ¢æ¤ÉµÄ¾³¢Bv·éÈçÈÆàinterfaceðvµÄ¾³¢B
- PHPÌinterfaceÍ»±ÜÅôû³ê½àÌÅÍÈ¢ÌÅúÒµ·¬ÄÍ¢¯Ü¹ñB
- öÌ^ðÌpµÄ¢éªä¦É½Æ©g¢¹ªcÁÄ¢éÌÅA_ðÅåÀpµÜµå¤B
- ÁÉ}[J[C^[tF[XÍPHPÅà®SÈ\Íðö·éÌÅAàÁÆÝñÈg¦Î¢¢Æv¢Ü·B
Ql¶£
interfaceÆ¢¦ÎJavaÅ·ÌÅAEffective Java ½èðÇÞÆ}X^[µ½´¶ÉÈêéÆv¢Ü·B½¾AâÅÉÈÁ¿áÁ½ñÅ·æ˦cB PHPÌðà{ɱÌèÌàeª¢Ä é±ÆÍHÈCªµÜ·B¼É¢¢{Ȣŵ天ËB
keyword: interface fUCp^[ OOP PHP