Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Definition of Abstract Class and Interface in PHP

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "the definition of abstract classes and interfaces in PHP". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the definition of abstract classes and interfaces in PHP.

Play with abstract classes and interfaces in PHP

In object-oriented development, especially in the process of using modern frameworks, we often deal with interfaces and abstract classes. Especially when we try to encapsulate some functions ourselves, interfaces and abstract classes are often the first step, but do you really know them?

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 "iTunm ChildA1";} public function getAge ($age) {echo "Age is". $age;}} class ChildA2 extends A {protected function getName () {echo "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 it will lead to a fatal error.

Interfaces can also be inherited by using the extends operator

Constants can also be defined in the interface. Interface constants and class constants are used exactly the same, but cannot be overridden by subclasses or subinterfaces

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 "iTunm 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 subclasses of an abstract class follow the inheritance principle and can only have one parent class, but a class can implement multiple interfaces

There can be non-abstract implemented methods in an abstract class; interfaces are full of abstract methods and method definitions

Access control for methods and variables in abstract classes is defined by itself; 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 USB 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 USB disk 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.

At this point, I believe you have a deeper understanding of "the definition of abstract classes and interfaces in PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report