Claims are not supported. A good example of class inheritance is the wp_http_response class, which is located in the WP include \/ rest API directory, but can be used for all types of HTTP responses. Extended by the wp\u rest\u response class specifically used to respond to rest API requests. The
Unless the abstract class is declared final, it can still be extended. PHP only provides abstract rules for classes that cannot be instantiated directly, and provides base classes for other classes that extend the rules. Another special rule for abstract classes is that abstract methods can be used. An abstract method must be redefined in the class that extends it, or an error will occur. This is a very useful system because you can define how subclasses work. The abstract method cannot have a body, and the method that redefines it cannot change the signature. If you try to change a parameter or parameter type in PHP 5 or PHP 7, a fatal error will occur. The
This is slightly different from redefining non abstract methods, but it is actually the same, because the signature of the method cannot be changed even if it can be changed on the technology that disables the strict standard PHP 5. Interfaces provide similar roles, but the methods of interfaces must be exposed, while abstract methods can be exposed, protected, or dedicated. Methods that redefine abstract methods must also use the same signature. Wp\u rest\u controller of WordPress rest API is a good example of abstract classes. All endpoints are extended to provide a standard system of how endpoint classes should work. The
Previous versions of PHP did not support abstract methods, so they were not used. Instead, the base class has a method that calls \u doing\u it\. This is the solution to the forced extension method. Another example is to connect to a social network through OAuth, and then consider a plug-in that stores public and private keys for later use. Because the actual key import is different, other code is required, but it can be reused to save and import the key. Therefore, we can write an abstract method called \
Abstract class society
Protected $public;
Protected $secret;
Public function \u construct ($public, $secret){
$this->public = $public;
$this->secret = $secret;
}The
Abstract public function connect();
Public function get_public(){
Return $this->public.
}The
Public function get\u secret(){
Return $this->secret.
}The
}This reduces code repeatability and applies specific patterns to the code. Here, all classes that extend this class have the same method to obtain the key, and can safely call the methods of all subclasses. The
For example, you could create a class called \
Connect public function (){
\/\/Do something to get the public key and set the correct properties.
}The
}The
Class twitter extends social capabilities. The{
Connect public function (){
\/\/Do something to get the public key and set the correct properties.
}The
}The
For
Each ([‘twitter’,’facebook’] as $social\u network){
\/**@Var social $obj*\/
$obj= new $social_network;
$obj-> connection ();
Update\u option (\
Update_option (\
}A brief introduction to PHP inheritance classes with less code and better code will help create smaller classes with more reusable code. Reusable code is not only more efficient, but also easier to read, easier to maintain, and requires fewer unit tests. Class inheritance has a variety of uses and requires actual browsing. Instead of reusing wp\u query, you can create a base class that calls wp\u query and extend it several times as needed. For example, if you build a plug-in that needs to customize the rest API endpoint, extending the wp\u rest\u controller class can save a lot of work. If you are working on a site that interacts with multiple transactional e-mail services, create a base class that handles all the common functionality required to interact with the transactional e-mail service API, and extend each service once. While you can continue with the example, you should review the existing code and project plan, consider how class inheritance skips copy \/ paste methods, and help improve your code.