PHP Object-Oriented Programming (oop) learning notes (1)

Source: Internet
Author: User
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 steps in software engineering

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 steps in software engineering

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:

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.