In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "detailed introduction of abstract classes and interfaces in PHP". In daily operation, I believe that many people have doubts about the detailed introduction of abstract classes and interfaces in PHP. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "detailed introduction of abstract classes and interfaces in PHP". Next, please follow the editor to study!
Abstract class definition
The characteristics of the abstract class:
As the name implies, it is abstract and, of course, cannot be instantiated. Therefore, abstract classes are generally defined as our base class. In a class, as long as a method is defined as abstract, the class must add the abstract keyword to become an abstract class. A method that is defined as abstract only declares how it is called and cannot define its specific functional implementation. The subclass must define all abstract methods in the parent class, and the access controls for these methods must be consistent with or more relaxed than the parent class. The way the method is called must match, that is, the type and the number of parameters required must be the same. Abstract methods implemented by subclasses can add parameters but must have default values. Abstract class A {
Public function show () {
$this- > getName ()
Echo PHP_EOL
}
Protected abstract function getName ()
Public abstract function getAge ($age)
}
Class ChildA1 extends A {
Public function getName () {
Echo "Ihumm ChildA1"
}
Public function getAge ($age) {
Echo "Age is". $age
}
}
Class ChildA2 extends A {
Protected function getName () {
Echo "Ihumm ChildA2"
}
Public function getAge ($age, $year ='') {
Echo "Age is". $age. ', bron'. $year
}
}
$ca1 = new ChildA1 ()
$ca1- > show ()
$ca1- > getAge (18)
$ca2 = new ChildA2 ()
$ca2- > show ()
$ca2- > getAge (20, 2000)
Interface definition
Features of the interface:
You can specify which methods a class must implement, but you do not need to define the specific contents of those methods. It's like defining a standard class, but all the methods defined are empty. All methods defined in the interface must be public, which is a feature of the interface. Class must implement all methods defined in the interface, or a fatal error will be reported. Class can implement multiple interfaces, separating the names of multiple interfaces with commas. For a class to implement an interface, it must be in exactly the same way as the methods defined in the interface. Otherwise, the fatal error interface can be inherited, and constants can also be defined by using the extends operator interface. Interface constants and class constants are used exactly the same, but cannot be overridden by subclasses or subinterfaces of interface B1 {
Const B1_NAME = 'Interface B1'
Function getName ()
Function getAge ($age)
}
Interface B2 extends B1 {
Function show ()
}
Interface B3 {
Function getSchool ()
}
Class ChildB implements B2, B3 {
Function getName () {
Echo "Ihumm ChildB"
}
Function getAge ($age, $year ='') {
Echo "Age is". $age
}
Function show () {
$this- > getName ()
Echo PHP_EOL
$this- > getAge (23, 1997)
Echo PHP_EOL
Echo self::B1_NAME
Echo PHP_EOL
}
Function getSchool () {
Echo "study in Peking University"
Echo PHP_EOL
}
}
B = new ChildB ()
$b-> show ()
$b-> getSchool ()
The difference between abstract classes and interfaces
From the above, we can summarize some differences between abstract classes and interfaces:
The subclass of an abstract class follows the principle of inheritance and can only have one parent class; but a class can implement multiple interfaces; an abstract class can have non-abstract implemented methods; the interface is full of abstract methods, which are defined by the access control of methods and variables in the abstract class; interfaces can only be public.
So the question is, which of these two goods is better? Sorry, there is no answer to this question. They serve a different purpose. Abstract classes can be used as base classes, providing public methods for subclasses, and customizing common abstractions for subclasses to implement. The interface is a higher level of abstraction, which allows us to rely on abstraction rather than concrete implementation, bringing more expansibility to software development.
Interface-oriented development
Interface, in fact, can also be seen as a contract. We often use the socket at the back of the main chassis of the computer to explain. For example, the USB interface, we define its size, the format of the line inside, no matter what you plug in, we can connect. The specific implementation depends on the explanation of the inserted hardware by the computer software. for example, the U disk will read its contents, and the keyboard will be identified as a peripheral.
It can be seen from here that the interface can provide a very strong support for the expansion of our program. Interfaces are very important features in any object-oriented language. Let's use the interface to simulate the USB Jack we just mentioned.
Interface USB {
Function run ()
}
Class Keyboard implements USB {
Function run () {
Echo, "this is the keyboard."
}
}
Class UDisk implements USB {
Function run () {
Echo "this is a flash drive"
}
}
What's the advantage of writing like this? Let's delve into one more concept: dependency injection. If you use interface-oriented development:
Function testUSB (USB $u) {
$u-> run ()
}
/ / insert flash drive
TestUSB (new UDisk)
/ / insert keyboard
TestUSB (new Keyboard)
The $u in the testUSB method is not a concrete instance, but an abstraction of the USB interface. Without knowing what instance it is, we guarantee that it must have a run () method through the interface contract. On the other hand, the specific implementation is injected when we call the method externally.
Summary
If you master the design principles of the interface, you can often understand the design ideas of more than half of the framework. This is also the most basic feature in our object-oriented. Abstract class as a common base class can provide a better template for polymorphism, it can give your subclass its own personality and the ability to use the parent class. In short, going deep into the scene business and choosing an appropriate way to implement the code depends on the combination of ability, experience and wisdom.
Test code: https://github.com/zhangyue0503/dev-blog/blob/master/php/201912/source/%E7%8E%A9%E8%BD%ACPHP%E4%B8%AD%E7%9A%84%E6%8A%BD%E8%B1%A1%E7%B1%BB%E4%B8%8E%E6%8E%A5%E5%8F%A3.php
Reference document: https://www.php.net/manual/zh/language.oop5.abstract.phphttps://www.php.net/manual/zh/language.oop5.interfaces.phphttps://www.php.net/manual/zh/language.oop5.interfaces.php#79110
At this point, the study of "detailed introduction of abstract classes and interfaces in PHP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.