Special classes, interface classes, and abstract classes in PHP (neither can be instantiated directly)

Source: Internet
Author: User

The interface class is not instantiated and requires one by one of all methods that implement the interface definition. Keyword interface implements

Interface interface is a rule that gives people inheritance with something that's kind of like abstract class
The method defined in the inside, but does not instantiate, but needs other classes to implements it, and must implement all the methods of the interface definition,
In fact, the interface class is plainly, is a class template, a class of provisions,


As An example ,
Interface Shop
{
Public function Buy ($gid);
Public function Sell ($gid);
Public function View ($gid);
}
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 one can not,
If the subclass does not implement these words, it cannot run.
In fact, the interface class is plainly, is a class template, a class of provisions,
If you are in this category, you have to follow my rules, less one can not, but specifically how you do, I do not care, it is your business, such as:

class Baseshop implements Shop
{
Public function Buy ($gid)
{
Echo (' You have purchased ID: '. $gid. ' Goods ');
}
Public function Sell ($gid)
{
Echo (' You sold the ID for: '. $gid. ' Goods ');
}
Public function View ($gid)
{
Echo (' You've looked at the ID: '. $gid. ' Goods ');
}
}
————————————————————————————————————————————————————————————————————————————————————————————————————————
Abstract class & Abstract method (can only be inherited) (keyword abstract) (inheritance keyword extends) The class with abstract method is abstract class, abstract method is empty method without content, require subclasses to perfect content, abstract class can not be instantiated, only inherit, By extends, the abstract class can also define the ordinary method of the parent class method execution is not deterministic, but the subclass also has this method Example 1: As a vehicle class: the definition of abstract methods to control the mode of transport, so that every vehicle, such as airplanes, cars have to rewrite the parent class method. If there is no point in defining the method in the parent class tool class (for example, walking on the ground), because every vehicle has to be rewritten (the plane wants to rewrite the method, the ship wants to rewrite the method), so for the uncertainty of your class method, we need an abstract method to implement polymorphism. Example 2: Define animal classes, each animal has a method of barking, but the surface is different, so to be defined as abstract class, let each animal class to achieve function. When the parent class is abstract, the subclass must override the abstract method of the parent class, but the abstract class must not necessarily write an abstract method, but a class that has an abstract method has to be defined as an abstract class that must inherit using an abstract method that cannot have a principal, {}<?phpabstract class abstractclass{/    /forcing subclasses to define these methods abstract protected function getValue ();     Abstract protected function Prefixvalue ($prefix);    Common method (non-abstract method) Public function PrintOut () {print $this->getvalue (); }}
________________________________________________________________________________________
















Special classes in PHP, interface classes, and abstract classes (neither can be instantiated directly)

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.