Introduction to PHPOPP mechanism and mode (abstract class, interface and contractual programming) _ PHP Tutorial

Source: Internet
Author: User
Introduction to PHPOPP mechanism and mode (abstract class, interface and contractual programming ). 1. in the abstract class mechanism, a common base class is always defined, and specific details are left to the successor for implementation. Through the abstract concept, you can create excellent scalability in the development project. 1. abstract class

In the abstract class mechanism, a public base class is always defined, and specific details are left to the successor for implementation. Through the abstract concept, you can create a scalable architecture in the development project. If at least one method in a class is declared as abstract, the class must be declared as abstract. An abstract method only declares its call method (parameters) and cannot define its specific function implementation. You can use the abstract modifier in the class declaration to declare a class as abstract.

1.1 prototype)

The signature after the method body is removed from the method definition. It includes the access level, function keywords, function names, and parameters. It does not contain ({}) or any code inside the brackets. For example, the following code is a method prototype:

The code is as follows:


Public function prototypeName ($ protoParam)

When inheriting an abstract class, the subclass must define all abstract methods in the parent class. In addition, the access control of these methods must be the same (or more loose) as in the parent class ).

1.2 Abstract classes

A class must be declared as an abstract class as long as it contains at least one abstract method.
An abstract method must contain the same or lower access level during implementation.
You cannot use the new keyword to create an instance of an abstract class.
Abstract methods cannot contain function bodies.
If you declare an extended class as an abstract class, you do not need to implement all abstract methods when extending the abstract class. (If a class inherits from an abstract class, it must be declared as abstract when it does not implement all the abstract methods declared in the base class .)

1.3 Use abstract classes

The code is as follows:


Abstract class Car
{
Abstract function getMaxSpeend ();
}

Class Roadster extends Car
{
Public $ Speend;

Public function SetSpeend ($ speend = 0)
{
$ This-> Speend = $ speend;
}
Public function getMaxSpeend ()
{
Return $ this-> Speend;
}
}

Class Street
{
Public $ Cars;
Public $ SpeendLimit;

Function _ construct ($ speendLimit = 200)
{
$ This-> SpeendLimit = $ speendLimit;
$ This-> Cars = array ();
}

Protected function IsStreetLegal ($ car)
{
If ($ car-> getMaxSpeend () <$ this-> SpeendLimit)
{
Return true;
}
Else
{
Return false;
}
}

Public function AddCar ($ car)
{
If ($ this-> IsStreetLegal ($ car ))
{
Echo 'The Car was allowed on The road .';
$ This-> Cars [] = $ car;
}
Else
{
Echo 'The Car is too fast and was not allowed on The road .';
}
}
}

$ Porsche911 = new Roadster ();
$ Porsche911-> SetSpeend (340 );

$ FuWaiStreet = new Street (80 );
$ FuWaiStreet-> AddCar ($ Porsche911 );

/**
*
* @ Result
*
* The Car is too fast and was not allowed on the road. [Finished in 0.1 s]
*
*/
?>

2. object interface

Using interfaces, you can specify the methods that a class must implement, but you do not need to define the specific content of these methods.

An interface is defined by the interface keyword, just like defining a standard class, but all methods are empty.

All methods defined in the interface must be public, which is a feature of the interface.

An interface is a structure similar to a class and can be used to declare the methods required to implement the class. For example, an API is usually used to declare an API without defining how to implement it.

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

2.1 Interfaces

Unlike the integrated abstract class, you need to use the extends keyword. the implemented interface uses the implements keyword. A class can implement multiple interfaces. in this case, we need to separate them with commas. If you mark a class as a method that implements an interface but does not implement this excuse, an error will be thrown.

2.2 Use Cases of interfaces

The code is as follows:


Abstract class Car
{
Abstract function SetSpeend ($ speend = 0 );
}

Interface ISpeendInfo
{
Function GetMaxSpeend ();
}

Class Roadster extends Car implements ISpeendInfo
{

Public $ Speend;

Public function SetSpeend ($ speend = 0)
{
$ This-> Speend = $ speend;
}

Public function getMaxSpeend ()
{
Return $ this-> Speend;
}
}

Class Street
{

Public $ Cars;
Public $ SpeendLimit;

Function _ construct ($ speendLimit = 200)
{
$ This-> SpeendLimit = $ speendLimit;
$ This-> Cars = array ();
}

Protected function IsStreetLegal ($ car)
{
If ($ car-> getMaxSpeend () <$ this-> SpeendLimit)
{
Return true;
}
Else
{
Return false;
}
}

Public function AddCar ($ car)
{
If ($ this-> IsStreetLegal ($ car ))
{
Echo 'The Car was allowed on The road .';
$ This-> Cars [] = $ car;
}
Else
{
Echo 'The Car is too fast and was not allowed on The road .';
}
}
}


$ Porsche911 = new Roadster ();
$ Porsche911-> SetSpeend (340 );

$ FuWaiStreet = new Street (80 );
$ FuWaiStreet-> AddCar ($ Porsche911 );

/**
*
* @ Result
*
* The Car is too fast and was not allowed on the road. [Finished in 0.1 s]
*
*/
?>

3. instanceof operator

The instanceof operator is a comparison operator in PHP5. It accepts parameters on both sides and returns a boolean value. This operator is used to determine whether an instance of an object is of a specific type, whether it is inherited from a certain type, or to implement a specific interface of the class.

The code is as follows:


Echo $ Porsche911 instanceof Car;
// Result: 1

Echo $ Porsche911 instanceof ISpeendInfo;
// Result: 1

4. contractual programming

Contractual programming is a programming practice that implements declarative interfaces before writing classes. This method is very useful in ensuring class encapsulation. Using contractual programming technology, we can define the features of View implementation before creating an application, which is very similar to the practice of architects drawing a blueprint before building the building.

5. Summary

Abstract classes are declared using abstract keywords. By marking a class as an abstract class, we can postpone the declared method. To declare a method as an abstract method, remove the method entity that contains all braces and end the code line of the method declaration with a semicolon.

Abstract classes cannot be directly instantiated. they must be inherited.

If a class inherits from an abstract class, it must be declared as abstract when it does not implement all the abstract methods declared in the base class.

In the interface, we can declare a method prototype without a method body, which is similar to an abstract class. The difference between them is that interfaces cannot declare any methods with method bodies, and they use different syntaxes. To force the unveiling rule to 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 of a specific class type or whether a specific interface is implemented. Instanceof is suitable for this task. Instanceof checks whether an instance is of a specific type, whether the instance is inherited from a specific type, or whether any of its ancestor classes implement class-specific interfaces.

Some languages have the ability to inherit from multiple classes, which is called multi-inheritance. PHP does not support multiple inheritance. Idea, which provides the function to declare multiple interfaces for a class.

This interface is useful when declaring rules that a class must follow. Contractual programming technology uses this function to enhance encapsulation and optimize workflows.

In the abstract class mechanism, a public base class is always defined, and specific details are left to the successor for implementation. Through the abstract concept, you can create excellent scalability in the development project...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.