How to use PHP interface class interface correctly

Source: Internet
Author: User
Tags php language learn php php template

For those who learn PHP language, for the PHP interface class may not understand very deep, then we will be specific to the PHP interface class interface use.

    • How to use PHP XmlReader to parse XML document correctly
    • In-depth interpretation of PHP Domxpath in XML file parsing
    • Featured several powerful PHP template engines
    • Key points of PHP usage tips
    • How to create an XML file correctly using PHP dom-xml

In fact, their role is very simple, when a lot of people to develop a project, may go to call someone else to write some of the classes, then you will ask, how do I know how his implementation of a function is named, this time the PHP interface class interface play a role, when we define an interface class, The way inside it is that the following subclass must be implemented, such as:

    1. Interface Shop
    2. {
    3. Public function Buy ($gid);
    4. Public function sell ($GID);
    5. Public Function view ($GID);
    6. }

I declare a shop interface class, defines three methods: Buy, Sell (Sell), see (view), then inherit all subclasses of this class must implement these 3 methods less than one, if the subclass does not implement these words, it will not run. In fact, the interface class is plainly, is a class template, a class of provisions, if you belong to this category, you have to follow my rules, less one can not, but specific how you do, I don't care, it is your business, such as:

    1. Class Baseshop implements Shop
    2. {
    3. Public function Buy ($gid)
    4. {
    5. Echo (' You have purchased ID: '. $gid. ' Goods ');
    6. }
    7. Public function Sell ($gid)
    8. {
    9. Echo (' You sold the ID for: '. $gid. ' Goods ');
    10. }
    11. Public Function view ($gid)
    12. {
    13. Echo (' You've looked at the ID: '. $gid. ' Goods ');
    14. }
    15. }

You think, in a multi-person cooperation of large projects, with the interface class is how convenient, so you do not have to ask others, your method name of the function is what, of course, if you like this I have no way.

Conclusion: PHP Interface class interface is the leader of a class, indicating the direction, the subclass must complete its specified method.

<?PHPInterfaceShop { Public functionBuy$gid);  Public functionSell$gid);  Public functionView$gid); }  classBaseshopImplementsShop { Public functionBuy$gid)      {      Echo(' You purchased the ID for: '.$gid.‘ of goods '); }       Public functionSell$gid)      {      Echo(' You sold the ID for: '.$gid.‘ of goods '); }       Public functionView$gid)      {      Echo(' You have looked at the ID: '.$gid.‘ of goods '); }  }  $haha=Newbaseshop ();$haha->buy (' 123 ');

Http://developer.51cto.com/art/200912/166303.htm

How to use PHP interface class interface correctly

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.