PHP Advanced Programming Learning Note 2014.06.09
This article describes the abstract classes, interfaces, and a technique called contract programming. Using these OPP mechanisms, the code you write is not limited to calculating or outputting content. These mechanisms can define the rules of interaction between classes at the conceptual level, and also provide the basis for application expansion and customization.
1. Abstract class
A common base class is always defined in the abstract class mechanism, and the specific details are left to the inheritors for implementation. Abstract concepts allow you to create well-scaled schemas in your development projects. any class, if at least one of its methods is declared abstract, then the class must be declared abstract. A method that is defined as abstract simply declares its invocation method (parameter) and cannot define its specific function implementation. You can declare a class as abstract by using the abstract modifier in the declaration of a class.
1.1 Method Prototypes (prototype)
means that the signature of the method body is removed from the definition of the method. It includes access levels, function keywords, function names, and parameters. He does not contain ({}) or any code inside the parentheses. For example, the following code is a method prototype:
Public function prototypename ($protoParam)
when inheriting an abstract class, the subclass must define all the abstract methods in the parent class, and in addition, the Access Control must be the same as (or more loosely) in the parent class.
1.2 About abstract Classes
- A class must be declared as an abstract class as long as it contains at least one abstract method
- Methods that are declared abstract must contain the same or lower access levels at the time of implementation.
- You cannot use the New keyword to create an instance of an abstract class.
- A method that is abstract by life cannot contain a function body.
- If you declare an extended class as an abstract class, you can extend the abstract class without implementing all of the abstract methods. ( If a class inherits from an abstract class, it must also be declared abstract when it does not implement all the abstract methods declared in the base class.) )
1.3 Using abstract Classes
<?PHPAbstract classcar{Abstract functiongetmaxspeend ();}classRoadster extends car{ Public $Speend; Public functionSetspeend ($speend= 0) { $this->speend =$speend; } Public functionGetmaxspeend () {return $this-Speend; }}classstreet{ Public $Cars ; Public $SpeendLimit ; function__construct ($speendLimit= 200) { $this-Speendlimit =$speendLimit; $thisCars =Array(); } protected functionIsstreetlegal ($car) { if($car->getmaxspeend () <$this-speendlimit) { return true; } Else { return false; } } Public functionAddcar ($car) { if($this->isstreetlegal ($car)) { Echo' The Car was allowed on the road. '; $this->cars[] =$car; } Else { Echo' The Car is too fast and were not allowed on the road. '; } }}$Porsche 911=NewRoadster ();$Porsche 911->setspeend (340);$FuWaiStreet=NewStreet (80);$FuWaiStreet->addcar ($Porsche 911);/** * * * @result * * The Car is too fast and am not allowed on the road. [Finished in 0.1s] **/?>
2. Object interface
Using interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specifics of these methods.
interface is through Interface keyword to define, just like defining a standard class, but all of the methods defined in it are empty.
All methods defined in the interface must be public, which is the attribute of the interface.
An interface is a class-like structure that declares a method that an implementation class must declare. For example, an interface is often used to declare an API without having to define how to implement the API.
Most developers choose to prefix the interface name with the capital letter I, which distinguishes it from the class in the code and in the generated document.
2.1 Using the interface
Unlike the extends keyword that is required to integrate abstract classes, the implementation interface uses the Implements keyword. A class can implement multiple interfaces, at which point we need to separate them with commas. If you mark a class as implementing an interface, but you do not have all the methods to implement that excuse, you will throw an error.
2.2 Examples of using interfaces
<?PHPAbstract classcar{Abstract functionSetspeend ($speend= 0);}Interfaceispeendinfo{functiongetmaxspeend ();}classRoadsterextendsCarImplementsispeendinfo{ Public $Speend; Public functionSetspeend ($speend= 0) { $this->speend =$speend; } Public functionGetmaxspeend () {return $this-Speend; }}classstreet{ Public $Cars ; Public $SpeendLimit ; function__construct ($speendLimit= 200) { $this-Speendlimit =$speendLimit; $thisCars =Array(); } protected functionIsstreetlegal ($car) { if($car->getmaxspeend () <$this-speendlimit) { return true; } Else { return false; } } Public functionAddcar ($car) { if($this->isstreetlegal ($car)) { Echo' The Car was allowed on the road. '; $this->cars[] =$car; } Else { Echo' The Car is too fast and were not allowed on the road. '; } }}$Porsche 911=NewRoadster ();$Porsche 911->setspeend (340);$FuWaiStreet=NewStreet (80);$FuWaiStreet->addcar ($Porsche 911);/** * * * @result * * The Car is too fast and am not allowed on the road. [Finished in 0.1s] **/?>
3.instanceof operator
The instanceof operator is a comparison operator in PHP5. He accepts both left and right arguments and returns a Boolean value. This operator is used to determine whether an instance of an object is a specific type, or whether to inherit from a type, or to implement a particular interface of a class.
Echo $Porsche 911 instanceof Car; // result:1 Echo $Porsche 911 instanceof Ispeendinfo; // result:1
4. Contract-Type programming
Contractual programming refers to a programming practice of implementing a declarative interface before writing a class. This method is useful in guaranteeing the encapsulation of a class. Using the contractual programming technique, we can define the function of the view implementation before creating the application, which is very similar to the way the architect paints the blueprint before building it.
5. Summary
Abstract classes are classes declared using the abstract keyword. By marking a class as an abstract class, we can defer implementing the declared method. To declare a method as an abstract method, simply remove the method entity containing all curly braces and end the line of code declared by the method with a semicolon.
Abstract classes cannot be instantiated directly, they must be inherited.
If a class inherits from an abstract class, it must also be declared abstract when it does not implement all the abstract methods declared in the base class.
In an interface, we can declare a method prototype without a method body, which is similar to an abstract class. The difference between them is that an interface cannot declare any method that has a method body, and they use a different syntax. To force the uncover rule to be added to a class, we need to use the Implements keyword instead of the extends keyword.
In some cases we need to determine whether a class is a type of a particular class, or whether a particular interface is implemented. Instanceof is divided into fit to complete this task. Instanceof checks three things: whether the instance is a specific type, whether the instance inherits from a particular type, whether the instance or any of his ancestor classes implement a class-specific interface.
Some languages have the ability to inherit from multiple classes, which is called multiple inheritance. PHP does not support multiple inheritance. Idea, he provides the ability to declare multiple interfaces for a class.
Interfaces are useful when declaring a rule that a class must follow. Contractual programming techniques use this functionality to enhance encapsulation and optimize workflow.