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

How to understand PHP object-oriented programming Abstract Class, object Interface, instanceof and contract programming

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

Share

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

This article mainly introduces "how to understand abstract classes, object interfaces, instanceof and contract programming of PHP object-oriented programming". In daily operation, I believe that many people have doubts about how to understand abstract classes, object interfaces, instanceof and contract programming of PHP object-oriented programming. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the doubts of "how to understand PHP object-oriented programming abstract classes, object interfaces, instanceof and contract programming"! Next, please follow the editor to study!

1. Abstract classes in PHP

PHP 5 supports abstract classes and abstract methods. Classes defined as abstract cannot be instantiated. Any class that has at least one method declared abstract must be declared abstract. A method defined as abstract simply declares how it is called (parameters) and cannot define its specific functional implementation. Use the abstract modifier in a class declaration to declare a class abstract.

It can be understood that the abstract class, as a base class, leaves specific details to the successor. By abstracting concepts, you can create scalable architectures in development projects.

The copy code is as follows:

Abstract class AbstractClass

{

Code...

}

1.1. Abstract methods

Use the abstract keyword to define abstract methods. Abstract methods retain only the method prototype (the signature after the method body is removed from the method definition), which includes access levels, function keywords, function names, and parameters. It does not contain ({}) or any code inside the parentheses. For example, the following code is an abstract method definition:

The copy code is as follows:

Abstract public function prototypeName ($protoParam)

When inheriting an abstract class, the subclass must define all abstract methods in the parent class; in addition, the access controls for these methods must be the same (or more relaxed) as in the parent class. In addition, the way the method is called must match, that is, the type and the number of parameters required must be the same.

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 be implemented with the same or lower level of access.

You cannot create an instance of an abstract class using the new keyword.

A method that is declared abstract cannot contain a function body.

If the extended class is also declared as an abstract class, you do not have to implement all abstract methods when extending the abstract class. 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. Use abstract classes

The copy code is as follows:

two。 Object interface

Using interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specific contents of those methods.

Interfaces are defined by the interface keyword, just 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.

An interface is a class-like structure that can be used to declare methods that must be declared to implement a class. For example, interfaces are typically used to declare an API without defining how to implement the API.

Most developers choose to prefix the interface name with an uppercase I to distinguish it from the class in code and in the generated documentation.

2.1Interface implementation (implements)

To implement an interface, using the implements operator (inheriting an abstract class requires a different extends keyword), the class must implement all the 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.

When implementing multiple interfaces, the methods in the interface cannot have duplicate names.

Interfaces can also be inherited by using the extends operator.

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.

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.

2.2 cases of using interfaces

The copy code is as follows:

3. Type operator instanceof

The instanceof operator is a comparison operator in PHP5. He takes parameters on the left and right sides and returns a Boolean value.

Determine whether a PHP variable belongs to an instance of a class of CLASS

Check whether the object inherits from a type

Check whether the object belongs to an instance of a class

Determine whether a variable is an instance of an object that implements an interface

The copy code is as follows:

Echo $Porsche911 instanceof Car

/ / result:1

Echo $Porsche911 instanceof ISpeendInfo

/ / result:1

4. Contract programming

Design by contract, or Design by Contract (DbC), is a method of designing computer software. This method requires software designers to define formal, accurate and verifiable interfaces for software components, which adds prior conditions, a posteriori conditions and invariants to traditional abstract data types. The "contract" or "contract" used in the name of this method is a metaphor because it is somewhat similar to the case of a commercial contract.

A programming practice of implementing a declared interface before writing a class. This method is very useful in ensuring the encapsulation of the class. Using contract programming techniques, we can define the functions achieved by the view before creating the application, which is very similar to the way architects draw blueprints before building buildings.

5. Summary

An abstract class is a class declared using the abstract keyword. By marking a class as abstract, we can defer implementing the declared method. To declare a method as an abstract method, simply remove the method entity that contains 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 very similar to an abstract class. The difference between them is that interfaces cannot declare any method that has a method body; and they use different syntax. In order to force the uncover rule on 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 specific interface is implemented. Instanceof is divided into suitable for this task. Instanceof checks for three things: whether the instance is a particular type, whether the instance inherits from a particular type, and whether the instance or any of its ancestors implements 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. It provides the ability to declare multiple interfaces for a class.

Interfaces are useful when declaring rules that a class must follow. Contract programming technology uses this feature to enhance encapsulation and optimize workflows.

At this point, the study on "how to understand PHP object-oriented programming abstract classes, object interfaces, instanceof and contractual programming" 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.

Share To

Development

Wechat

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

12
Report