How to properly use the PHP interface class interface

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:

Interface Shop  {public  function buy ($gid);  Public function sell ($gid);  Public Function view ($gid);  }

Declares 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 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:

Class Baseshop implements  shop {public  function buy ($gid)  {  echo (' You have purchased the ID: '. $gid. ');  }  Public function Sell ($gid)  {  echo (' You sold the ID: '. $gid. ');  }  Public Function View ($gid)  {  echo (' You have viewed the item ID: '. $gid. ');}  }

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.

<?phpinterface Shop  {public      function buy ($gid);      Public function sell ($gid);      Public Function view ($gid);  }  Class Baseshop implements  shop {public              function buy ($gid)      {      echo (' You have purchased the ID: '. $gid. ');      } Public      function Sell ($gid)      {      echo (' You sold the ID: '. $gid. ');      }      Public Function View ($gid)      {      echo (' You have viewed the item ID: '. $gid. ');}  }  $haha = new Baseshop (); $haha->buy (' 123 ');

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.