PHP object-oriented programming (oop) learning Notes (1)-abstract classes, object interfaces, instanceof and contractual programming _ php instances

Source: Internet
Author: User
Tags define abstract
Object-oriented programming (OOP) is a computer programming architecture. One basic principle of OOP is that a computer program is composed of a single unit or object that can act as a subroutine. OOP has achieved three main objectives of Software Engineering: reusability, flexibility, and scalability. To achieve the overall operation, each object can receive, process, and send information to other objects. 1, Abstract classes in PHP

PHP 5 supports abstract classes and abstract methods. Classes defined as abstract cannot be instantiated. 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 class declaration to declare a class as abstract.

It can be understood that the abstract class, as a base class, leaves specific details to the successor for implementation. Through the abstract concept, you can create a scalable architecture in the development project.

The code is as follows:


Abstract class AbstractClass
{
Code...
}

1.1 Abstract methods

Use abstract keywords to define abstract methods. The abstract method only retains the method prototype (the signature after the method body is removed in 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 an abstract method definition:

The 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 control of these methods must be the same (or more loose) as in the parent class ). In addition, the call method of the method must match, that is, the type and the number of required parameters must be consistent.

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.
Declared 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 interface implementation (implements)

To implement an interface, use the implements operator (different extends keywords are required to inherit abstract classes). the class must implement all methods defined in the interface; otherwise, a fatal error is reported. Class can implement multiple interfaces, separated by commas.

When multiple interfaces are implemented, the methods in the interface cannot have duplicate names.
Interfaces can also be inherited by using the extends operator.
To implement the interface, you must use the method exactly the same as the method defined in the interface. Otherwise, a fatal error occurs.
Constants can also be defined in the interface. The use of interface constants and class constants is identical, but they cannot be overwritten by the quilt class or sub-interface.
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,Type operator instanceof

The instanceof operator is a comparison operator in PHP5. It accepts parameters on both sides and returns a boolean value.

Determines whether a PHP variable belongs to an instance of a CLASS.
Check whether the object inherits from a certain type.
Check whether the object belongs to an instance of a class
Determines whether a variable is an instance of an object that implements an interface.

The code is as follows:


Echo $ Porsche911 instanceof Car;
// Result: 1

Echo $ Porsche911 instanceof ISpeendInfo;
// Result: 1

4.Contractual programming

Contractual Design or Design by Contract (DbC) is a method for designing computer software. This method requires software designers to define formal, accurate, and verifiable interfaces for software components. in this way, a prior condition, posterior condition, and non-variant form are added for traditional abstract data types. The "contract" or "contract" used in the name of this method is a metaphor, because it is a bit similar to the commercial contract.

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.

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.