"getting started with PHP object-oriented (OOP) programming" 20.PHP5 Interface Technology (interface)

Source: Internet
Author: User
<span id="Label3"></p><p><p>Like most object-oriented programming languages, PHP does not support multiple inheritance. This means that each class can inherit only one parent class. To solve this problem, PHP introduced the interface, the idea of the interface is to specify a implementation of the interface of the class must implement a series of METHODS. <strong>interface is a special kind of abstract class, abstract class is a special class, so the interface is a special class,</strong> Why is the interface is a special kind of abstract class? If all of the methods in an abstract class are abstract, then we use the "interface" in a different way, that is, all the methods in the interface must be declared as abstract methods, and the interface cannot declare variables (but can declare constant constant). And all the members of the interface are public permissions. So the subclass must also use the public permission limit when implementing it.</p></p><p><p>Declare a class when we use the keyword is "class", and the interface of a special class, the use of the keyword is "<strong>interface</strong>";</p></p> <blockquote> <blockquote> class Definition: class name {...}, <br>Declaration of the Interface: Interface interface name {...} </blockquote> </blockquote><pre class="brush:php;"><pre class="brush:php;"><?php//defines an interface using the interface keyword, "one" defines a constant const constant = ' constant value ' for the interface name interface one{//, And//defines an abstract method "fun1" Public Function Fun1 ();//defines the abstract method "fun2" Public Function Fun2 ();}? ></pre></pre><p><p>The above example defines an interface "one", which declares two abstract methods "fun1" and "fun2", because all of the methods in the interface are abstract methods, so when declaring an abstract method, it is not necessary to use the "abstract" keyword as an abstraction. The default has been added to this keyword, in addition to the interface inside of the "public" This access can also be removed, because the default is public, because all members of the interface if the member of the interface, we can not use "private" and "protected "permission to use public or default. In addition we declare a constant "constant" inside the interface, because the variable member cannot be used in the interface, so we want to use the Const keyword Declaration.</p></p><p><p>Because the interface is a special kind of abstract class, all of the methods are abstract methods, so the <strong>interface can not produce an instance object</strong> ; It is also done as a specification, and all abstract methods require subclasses to be implemented.</p></p><p><p>We can use the "<strong>extends</strong>" keyword to get an interface to inherit another interface:</p></p><pre class="brush:php;"><pre class="brush:php;"><?php//uses "extends" to inherit another interface interface two extends One{function fun3 (); function Fun4 ();}? ></pre></pre><p><p>And we define the subclass of an interface to implement the key words that all abstract methods in the interface use are <strong>"implements</strong>", rather than "extends" as we said earlier;</p></p><pre class="brush:php;"><pre class="brush:php;"><?php//uses the keyword "implements" to implement an abstract method interface between interfaces and classes between class three implements One{function fun1 () {...} function Fun2 () {...}} Implement all the methods, we can use subclasses to instantiate the object $three = new three (); ></pre></pre><p><p>We can also use abstract classes to implement some of the abstract methods in the interface, but to instantiate an object, the abstract class must have subclasses to implement all of its abstract methods;</p></p><p><p>As we said earlier, PHP is single-inheritance, A class can have only one parent class, but a class may implement more than one interface, it is equivalent to a class to comply with a number of specifications, just as we have to abide by the laws of the state, if it is in school, but also abide by the school rules;</p></p><pre class="brush:php;"><pre class="brush:php;"><?php//uses implements to implement multiple interfaces Class four Implemtns interface one, interface two, ... {//you must implement all the methods in the interface to instantiate the Object. }?></pre></pre><p><p><strong>In php, not only a class can implement multiple interfaces, but also can inherit a class at the same time to implement multiple interfaces, we must first inherit the class to implement the interface;</strong></p></p><pre class="brush:php;"><pre class="brush:php;"><?php//uses extends to inherit a class, using implements to implement multiple interfaces class four extends class name one Implemtns interface one, interface two, ... {//all methods in the interface must be implemented to instantiate the object ...}? ></pre></pre><p><p>"getting started with PHP object-oriented (OOP) programming" 20.PHP5 Interface Technology (interface)</p></p></span>

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.