Home > Article > Backend Development > Example explanation of PHP interface features_PHP tutorial
We are learning the VideoCard.php interface file (functional interface definition of the graphics card)
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>interface VideoCardInter{ </SPAN><LI class=alt><SPAN> function Display(); </SPAN><LI class=""><SPAN> function getName(); </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
Dmeng.php implements the interface (the manufacturers of Diamond have implemented these interfaces. How to implement them is not a matter of the motherboard manufacturer)
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>include_once("VideoCard.php"); </SPAN><LI class=alt><SPAN>class Dmeng implements VideoCardInter { </SPAN><LI class=""><SPAN> function Display(){ </SPAN><LI class=alt><SPAN> echo "Display"; </SPAN><LI class=""><SPAN> } </SPAN><LI class=alt><SPAN> function getName(){ </SPAN><LI class=""><SPAN> return "Dmeng VideoCard"; </SPAN><LI class=alt><SPAN> } </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
Mainboard.php application interface (insert the graphics card into the motherboard, the motherboard only needs to use these interfaces, or not)
<ol class="dp-xml"> <li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>include_once("VideoCard.php"); </SPAN><LI class=alt><SPAN>include_once("Dmeng.php"); </SPAN><LI class=""><SPAN>class Mainboard{ </SPAN><LI class=alt><SPAN> var $vc; </SPAN><LI class=""><SPAN> function run(VideoCardInter $vc){ //定义VideoCardInter接口类型参数,这时并不知道是谁来实现。 </SPAN><LI class=alt><SPAN> $this-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span class="attribute"><font color="#ff0000">vc</font></span><span>=$vc; </span> </li> <li class=""> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>vc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>Display(); </span> </li> <li class="alt"><span> echo "主板运行!"; </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">conputer</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Mainboard(); </span> </li> <li class="alt"> <span>$conputer-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>run(new Dmeng); //用的时候把实现接口类的名称写进来,<br>(现在是帝盟的显卡,也可以换成别的场家的,只要他们都实现了接口) </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
Since PHP is a dynamic language, the type cannot be like JAVA The definition is very rigid. When defining the interface, it will make an error to write the return type. I guess in PHP6, the definition may be more strict.
PS: I only wrote the most basic part of the PHP interface features, and you can also add CPU and other interfaces.