In PHP, the interface abstract class, final, static a few of the fairly simple we used, especially the large Web site architecture will be useful, today we look at an abstract class in PHP, final, static example.
1. Interface
(1) The use of the interface is through the keyword implements
(2) Interfaces cannot define member variables (including class static variables) and can define constants
(3) Subclasses must implement all the methods of the interface definition
(4) The interface can only define that the method cannot be implemented
(5) interface has no constructors
(6) The method in the interface and the class that implements it are the public type by default
2. Abstract class
(1) The use of abstract classes is through the keyword extends
(2) cannot be instantiated, you can define a method that subclasses must implement
(3) Subclasses must define all the abstract methods in the parent class, which must have the same access control as (or more loosely) in the parent class
(4) If there is an abstract method in a class, the class must be defined as an abstract class
(5) Abstract classes can have constructors
(6) Methods in an abstract class can be modified using Private,protected,public.
(7) A class can implement multiple interfaces at the same time, but a class may inherit only one abstract class.
3. Final class/method
(1) Final class cannot be inherited
(2) Final method cannot be rewritten
4. Static Class/method
(1) You can access directly without instantiating a class
(2) A static property cannot be accessed by an object through the operator, using:: Method call
Example
<?php# Interface interface human{const TEST_CONST = "TEST const";//define Constants//public $v;//error, cannot define variable//static $co Unt Error, cannot define variable public function speak (); Public function walk (); Public function Run ();} # abstract class Father implements human{public function construct () {echo "Father init n"; } Abstract Public Function walk (); Abstract method Public Function speak () {echo "Father speak skill N"; } public Function run () {echo "Father run skill N"; }} # Non-abstract classes class mother implements human{Public function construct () {echo "Mother init n"; } # must be implemented here Walk method public Function walk () {echo "Mother walk skill N"; } public Function speak () {echo "Mother speak skill N"; } public Function run () {echo "Mother run skill N"; }} class Son extends father{public Function walk () {echo "Son walk skill. n "; } PublIC function speak ($name = ') {echo "son:". $name. "Speak skill. n "; The access control must be the same as in the parent class (or looser) protected function sport () {echo ' son sport skill. n "; Final public Function Notteach () {echo ' son have not to teach skill '; }} class daughter extends mother{public function run ($name = ') {echo ' daughter run skill. n "; }} Final class grandchild extends son{# Access control must be the same as in the parent (or more relaxed) public function sport () {echo "Grandchild sport skill. n "; } # Cannot override final method Son::notteach ()//Public Function Notteach () {}//error} # Class orphan May isn't inherit from final class (grandchild)//class Orphan extends grandchild{}//Error $son = new Son (); $son Speak ("suly"); $daughter = new daughter (); $daughter->run (' Lily '); $grandChild = new grandchild (); $grandChild->sport ();
The difference between an abstract class and an interface
1, the use of the interface is through the keyword implements. The use of abstract classes is through the keyword extends. Of course the interface can also be inherited by the keyword extends.
2. You cannot declare member variables (including class static variables) in an interface, but you can declare class constants. Abstract classes can declare various types of member variables to implement the encapsulation of data. (The member variables in the Java interface are declared as public static final type)
3, interfaces do not have constructors, abstract classes can have constructors.
4, the method in the interface is the public type by default, and the methods in the abstract class can be decorated with private,protected,public.
5. A class can implement multiple interfaces at the same time, but a class may inherit only one abstract class.
An abstract class or an interface.
If you are creating a model, this model will be used by some closely related objects, and you can use abstract classes. The interface is used if you want to create features that will be used by some unrelated objects.
If the behavior must be inherited from multiple sources, the interface is used.
If you know that all classes share a common behavior implementation, use an abstract class and implement that behavior in it.