Home  >  Article  >  Backend Development  >  PHP object-oriented (interface) basic concepts and abstract class examples introduction

PHP object-oriented (interface) basic concepts and abstract class examples introduction

伊谢尔伦
伊谢尔伦Original
2017-07-08 09:16:021099browse

What is an interface (interface)?

Interface is the abstraction of methods. If different classes have the same methods, then you should consider using interfaces.
(1) An interface is a behavioral specification and protocol. In fact, it is an agreement and a constraint between classes. (2) C# does not support multiple inheritance, but it leaves this function to the interface for implementation.
(3) The system resource calling methods between classes are different, making communication between them difficult, but the interface can shield the differences between them and enable them to communicate smoothly.

(4) You can specify which methods a certain class must implement, but you do not need to define the specific content of these methods.


What is

abstract class(abstractclass)?

1. An abstract class provides only a partial implementation of a type. Abstract classes can have instance variables, and one or more

constructors. An abstract class can have both abstract methods and concrete methods. An abstract class will not have instances, and these constructors cannot be called by clients to create instances. The constructor of an abstract class can be called by its subclasses, so that all subclasses of an abstract class can have some common implementations, and different subclasses can have their own implementations based on this.

2. Purpose of abstract classes 1) Concrete classes are not used for inheritance. Scott Meyers once pointed out that whenever possible, avoid inheritance from concrete classes. 2) Suppose there are two concrete classes, class A and class B. Class B is a subclass of class A. Then the simplest modification plan is to create an abstract class (or java interface) C, and then let class A and class B becomes a subclass of abstract class C. 3) Abstract classes should have as much common code as possible. To improve code reuse rate. 4) Abstract classes should have as little data as possible.

3. Patterns and principles based on abstract classes 1) Program for abstractions, not for specific programming. 2) Try to use composition (Com

position) instead of inheritance to achieve reuse. 3) Use the template method mode

4. When should inheritance and reuse be used? 1) A subclass is a special category of the superclass, not a role of the superclass. That is to say, two relationships must be distinguished: "is-a" and "has-a". 2) There will never be a situation where you need to replace a subclass with another subclass. If the designer is not sure that a class will not become a subclass of another class in the future, the class should not be designed to be a subclass of the superclass. 3) Subclasses have the responsibility to extend the superclass, but not to replace (Override) or cancel (Nullify) the superclass. 4) Inheritance can only be used when it makes sense in taxonomy, not tool class inheritance.

Abstract methods are methods that must be implemented. And only in abstract classes.

Interfaces and abstract classes

A class can inherit multiple interfaces. . .

A class can only inherit one abstract class. . .

Abstract methods are methods that must be implemented. Just like animals have to breathe. But fish breathe with their gills and pigs with their lungs.

Animals must have breathing methods. How to breathe is a matter of subclasses.

There are many discussions and suggestions advocating using interface instead of abstract class. The two can be mixed in theory, but in practical applications, they still have certain differences. Abstract classes generally serve as public parent classes to provide the basis for extensions of subclasses. The extensions here include

attributesand behaviors. Generally speaking, interfaces do not consider attributes, only methods, so that subclasses can freely fill in or extend the methods defined by the interface. As the JAVA prince said, the adapter in event is a good one. application. Using a simple example, such as a teacher, we treat it as an abstract class with its own attributes, such as age, education level, teacher number, etc., and teachers are also divided into many types, we can Inherit the teacher class and extend the unique category attributes, while the universal attributes have been directly inherited.
As for the interface~ Let’s take teachers as an example. Teachers have many behaviors. In addition to the same behaviors as ordinary people, there are also career-related behaviors, such as correcting test papers, giving lectures, etc. We define these behaviors as bodyless. Methods, as a collection, are an interface. Teachers Zhang San and Li Si have different behavioral characteristics, so they can expand their behavioral bodies. In this sense, interface focuses on behavior.
In short, in many cases, interfaces can indeed replace abstract classes, if you do not need to deliberately express inheritance on properties.

The interface defines the general specification for implementing a certain service, declaring the required functions and constants, but does not specify how to implement it. The reason why implementation details are not given is because different entities may need to implement public method definitions in different ways. The key is to establish a set of general principles that must be implemented. Only when these principles are met can the interface be said to be implemented.

Class members are not defined in the interface! The definition of class members is completely left to the implementation class.

Inherit more interfaces

<span style="color:#000000"><span style="color:#0000bb"></span></span>##

<?php  
interface  a{  
    public function  foo ();  
}  
interface  b{  
    public function  bar ();  
}  
//一个接口类可以继承多个接口  
interface  c  extends  a ,  b{  
    public function  baz ();  
}  
  
class  d  implements  c  
{  
    public function  foo (){  
    }  
    public function  bar (){  
    }  
    public function  baz (){  
    }  
}  
?>

Not only one class in PHP can implement multiple Interfaces can also implement multiple interfaces while inheriting a class. You must first inherit the class and then implement the interface



The above is the detailed content of PHP object-oriented (interface) basic concepts and abstract class examples introduction. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn