Examples of PHP interface features

Source: Internet
Author: User

We are learningVideoCard. php interface file (function interface definition of the video card)

 
  1. <?php 
  2. interface VideoCardInter{  
  3.     function Display();  
  4.     function getName();  
  5. }  
  6. ?> 

Dmeng. php implementation interface (how can the emon manufacturer implement these interfaces? The motherboard manufacturer does not need to care about them)

 
  1. <?php 
  2. include_once("VideoCard.php");  
  3. class Dmeng implements VideoCardInter {  
  4.     function Display(){  
  5.         echo "Display";  
  6.     }  
  7.     function getName(){  
  8.         return "Dmeng VideoCard";  
  9.     }  
  10. }  
  11. ?> 

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)

 
  1. <?Php 
  2. Include_once ("VideoCard. php ");
  3. Include_once ("Dmeng. php ");
  4. Class Mainboard {
  5. Var $ vc;
  6. Function run (VideoCardInter $ vc) {// defines the VideoCardInter interface type parameter. At this time, you do not know who will implement it.
  7. $ This->Vc= $ Vc;
  8. $ This->Vc->Display ();
  9. Echo "main board running! ";
  10. }
  11. }
  12. $Conputer=NewMainboard ();
  13. $ 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)
  14. ?> 

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.