We are learningVideoCard. php interface file (function interface definition of the video card)
- <?php
- interface VideoCardInter{
- function Display();
- function getName();
- }
- ?>
Dmeng. php implementation interface (how can the emon manufacturer implement these interfaces? The motherboard manufacturer does not need to care about them)
- <?php
- include_once("VideoCard.php");
- class Dmeng implements VideoCardInter {
- function Display(){
- echo "Display";
- }
- function getName(){
- return "Dmeng VideoCard";
- }
- }
- ?>
Mainboard. php application interface (plug the video card into the motherboard, the motherboard only needs to use these interfaces, you can also do not need)
- <?Php
- Include_once ("VideoCard. php ");
- Include_once ("Dmeng. php ");
- Class Mainboard {
- Var $ vc;
- Function run (VideoCardInter $ vc) {// defines the VideoCardInter interface type parameter. At this time, you do not know who will implement it.
- $ This->Vc= $ Vc;
- $ This->Vc->Display ();
- Echo "main board running! ";
- }
- }
- $Conputer=NewMainboard ();
- $ Conputer->Run (new Dmeng); // when used, write the name of the implemented interface class,
(The video card of EMR can be switched to another home, as long as they all implement interfaces)
- ?>
Because PHP is a dynamic language, the type cannot be set as hard as JAVA. When defining an interface, writing the return type may lead to an error. It is estimated that PHP 6 may be more rigorous in definition.
PS: I only write the most basic part of the PHP interface features, and can also add interfaces such as CPU.