PHP interface and abstract class usage sample code

Source: Internet
Author: User
Tags what interface
Abstract class

1. An abstract class is a class that has an abstract keyword in front of the class and an abstraction method (which adds the abstract keyword before the class method function keyword).

2. Abstract classes cannot be instantiated directly. Only the methods required for defining (or partially implementing) subclasses in an abstract class. Subclasses can make abstract classes materialized by inheriting abstract classes and by implementing all the abstract methods in the abstract class.

3. If a subclass needs to be instantiated, it implements all the abstract methods in the abstract class. If the subclass does not fully implement all the abstract methods in the abstract class, then the subclass is an abstract class that must precede the class with the abstract keyword and cannot be instantiated.

The code is as follows:

Abstract class A  {      /** can be defined as variable */     protected $value 1 = 0;      Private $value 2 = 1;      Public $value 3 = 2;      /** can also define non-abstract methods *     /Public Function my_print ()      {          echo "hello,world/n";      }      /**      * In most cases, an abstract class contains at least one abstract method. Abstract methods are declared with the abstract keyword, which cannot have specific content.      * You can declare an abstract method as you would declare a normal class method, but end with a semicolon instead of a method body. This means that abstract methods cannot be implemented in abstract classes, that is, there is no function body "{Some codes}".      *     /abstract protected function abstract_func1 ();      Abstract protected function Abstract_func2 ();  }  Abstract class B extends  A  {public      function abstract_func1 ()      {         echo "implement the Abstract_ Func1 in class a/n ";      }      /** this in Zend Studio 8 will be error *     ///abstract protected function Abstract_func2 ();  }  Class C extends B  {public      function Abstract_func2 ()      {         echo ' Implement the Abstract_func2 in Class A n ";      }  }

4. If you create a subclass B that inherits from a as follows, but does not implement the abstract Method Abstract_func ():

The code is as follows:

Class B extends a{};

Then the program will appear with the following error:

The code is as follows:

Fatal Error:class B contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (A::abstract_func)

5. If b implements the abstract method Abstract_func (), then the access control of the Abstract_func () method in B cannot be more restrictive than the access control of Abstract_func () in A, which means:

(1) if Abstract_func () in A is declared public, then the declaration of Abstract_func () in B can only be public, not protected or private

(2) if Abstract_func () in A is declared as protected, then the declaration of Abstract_func () in B can be public or protected, but not private

(3) if Abstract_func () in a is declared private, hehe, can not be defined as private Oh! (Fatal error:abstract function A::abstract_func () cannot be declared private)

Second, interface interface

1. Abstract classes provide a concrete implementation of the standard, and the interface is purely a template. The interface defines only the functionality, not the content of the implementation. The interface is declared with the keyword interface.

2. Interface are completely abstract, can only declare methods, and can only declare methods of public, cannot declare private and protected methods, cannot define method bodies, and cannot declare instance variables. However, interface can declare constant variables. But putting constant variable in interface violates its purpose of being an interface, and it confuses the different value of interface and class. If you really need it, you can put it in the appropriate abstract class or class.

The code is as follows:

Interface IA  {      const avar=3;      Public function iAfunc1 ();      Public function IAfunc2 ();  }  echo IA:: AVAR;

3. Any class that implements an interface implements all the methods defined in the interface

The code is as follows:

Class E implements IA  {public      function iAfunc1 () {echo ' in IAfunc1 ';}      Public Function IAfunc2 () {echo ' in IAfunc2 ';}  }

Otherwise, the class must be declared abstract.

The code is as follows:

Abstract class E implements ia{}

4. A class can use the Implements keyword in a declaration to implement an interface. After doing so, the exact process of implementing the interface is the same as inheriting an abstract class that contains only abstract methods. A class can inherit a parent class and implement any number of interfaces at the same time. The extends clause should precede the implements clause. PHP only supports inheriting from a parent class, so the extends keyword can only be followed by a class name.

The code is as follows:

Interface IB {public function iBfunc1 ();  Public function IBfunc2 (); } class D extends A implements Ia,ib {public Function abstract_func1 () {echo "Implement the abstract      _func1 in class a/n ";      } public Function Abstract_func2 () {echo ' Implement the Abstract_func2 in class a/n;      } public Function IAfunc1 () {echo ' in IAfunc1 ';}      Public Function IAfunc2 () {echo ' in IAfunc2 ';}      Public Function iBfunc1 () {echo ' in IBfunc1 ';}  Public Function IBfunc2 () {echo ' in IBfunc2 ';}         } class D extends B implements Ia,ib {public Function abstract_func1 () {parent::abstract_func1 ();      echo "Override the Abstract_func1 in class a/n";      } public Function Abstract_func2 () {echo ' Implement the Abstract_func2 in class a/n;      } public Function IAfunc1 () {echo ' in IAfunc1 ';}      Public Function IAfunc2 () {echo ' in IAfunc2 ';}      Public Function iBfunc1 () {echo ' in IBfunc1 ';} Public function IBfunc2 () {echo "in IBfunc2";} }

5. Interfaces can not implement another interface, but can inherit multiple

The code is as follows:

Interface IC extends ia,ib{}  class F implements IC  {public      function iAfunc1 () {echo ' in IAfunc1 ';}      Public Function IAfunc2 () {echo ' in IAfunc2 ';}      Public Function iBfunc1 () {echo ' in IBfunc1 ';}      Public Function IBfunc2 () {echo ' in IBfunc2 ';}  }

Iii. similarities and differences of abstract classes and interfaces

1. The same point:

(1) Both are abstract classes and cannot be instantiated.

(2) The subclass of interface implementation class and abstract class must implement an abstract method that has already been declared.

2. Different points:

(1) interface need to implement, to use implements, and abstract class need to inherit, to use extends.

(2) A class may implement multiple interface, but a class can inherit only one abstract class.

(3) interface emphasizes the realization of a particular function, while the abstract class emphasizes its affiliation.

(4) Although the subclass of the interface implementation class and the abstract class must implement the corresponding abstract method, the implementation is different in form. Each method in interface is an abstract method, all just declared (declaration, no method body), the implementation class must be implemented. The subclass of abstract class can be implemented selectively. This option has a two point meaning: a) Not all methods in abstract class are abstract, only those whose crowns have abstractions are abstract and must be implemented by subclasses. Those that have no abstract method, must define the method body in the abstract class, B) The subclass of abstract class inherits it, the non-abstract method can be either directly inherited or overwritten, and the abstract method can be chosen to implement, but also left to its subclasses to implement, However, this class must also be declared as an abstract class. Abstract classes, and of course, cannot be instantiated.

(5) Abstract class is an intermediary between interface and class. Abstract class plays a connecting role in interface and class. On the one hand, abstract class is abstract and can be used to declare abstractions to standardize the functions that subclasses must implement, on the other hand, it can define the default method body for the subclasses to use or overwrite directly. In addition, it can define its own instance variables for use by subclasses through inheritance.

(6) Before the abstract method in the interface can not add the abstract keyword, the default implicit is an abstract method, and can not add the final keyword to prevent the inheritance of abstract methods. Abstract methods in abstract classes must be preceded by an abstract representation to display the declaration as an abstraction.

(7) The abstract method in the interface is public by default, and can only be public, and cannot be decorated with the private, protected modifier. Abstract methods in abstract classes can be modified with public, protected, but not private.

3. Interface applications

(1) Classes and classes require a specific interface to reconcile, regardless of how they are implemented.

(2) as an identity that is capable of achieving a specific function, it can also be a purely identifier of what interface method does not exist.

(3) A set of classes needs to be treated as a single class, and the caller is only connected to this set of classes through an interface.

(4) A specific number of functions need to be implemented, and there may be no connection between these features at all.

4. Application of abstract class

In a word, it can be used in situations where both a uniform interface is required and an instance variable or a default method is required. The most common are:

(1) Defines a set of interfaces, but does not want to force each implementation class to implement all interfaces. You can use the abstract class to define a set of method bodies, even an empty method body, and then select the methods that you are interested in to overwrite by subclasses.

(2) On some occasions, purely interfaces do not satisfy the coordination between classes and classes, and the variables that represent states in a class must differentiate between different relationships. Abstract mediation can be a good way to meet this point.

(3) standardize a set of mutually coordinated methods, some of which are common, state-independent, shareable, without subclasses, and others require individual subclasses to implement specific functions according to their specific state.

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.