PHP abstract class Introduction _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP abstract class introduction. In natural language, we understand the abstract concept that a large description of an object is a common feature for a certain type of object. The same is true in PHP. in natural language, we understand the abstract concept as a large description of an object, this description is a common feature for certain objects. The same is true in PHP. We abstract a class to specify the general behavior of the class. this class should be a template, it indicates some actions that must be implemented by its sub-methods.
PHP abstract class Application definition:
Abstract class ClassName {
}

Key points of PHP abstract class application:
1. define some methods. subclass must fully implement all methods in this abstract.
2. an object cannot be created from an abstract class. Its significance lies in being extended.
3. abstract classes usually have abstract methods without braces in the methods

PHP abstract class application focus:
1. abstract methods do not need to implement specific functions, and are completed by subclasses.
2. when a subclass implements an abstract class method, its subclass visibility must be greater than or equal to the definition of an abstract method.
3. the abstract class method can have parameters or be empty.
4. if the abstract method has parameters, the implementation of the subclass must have the same number of parameters.

PHP abstract class application example:
Abstract public function_name (); // note that no braces exist
PHP abstract class rules:
A class must be declared as an abstract class as long as it contains at least one abstract method.
Abstract method, cannot contain function bodies
Inherit the subclass of the abstract class and implement the abstract method. it must have the same or lower access level as the abstract method.
Inherits the subclass of an abstract class. if not all abstract methods are implemented, this subclass is also an abstract class.
As a demonstration, we can implement a simple abstract class: calculate the area of the rectangle. This rectangle can be extended from the shape class.

The code is as follows:


<? PHP
Abstract class Shape {
Abstract protected function get_area ();
// Unlike the general method, this method does not have braces
// You cannot create an instance of this abstract class: $ Shape_Rect = new Shape ();
}
Class Rectangle extends Shape {
Private $ width;
Private $ height;
Function _ construct ($ width = 0,
$ Height = 0 ){
$ This-> width = $ width;
$ This-> height = $ height;
}
Function get_area (){
Echo ($ this-> width + $ this-> height) * 2;
}
}
$ Shape_Rect = new Rectangle (20, 30 );
$ Shape_Rect-> get_area ();
?>


This is also a simple example. it can basically describe the usage of abstract classes in PHP. I personally think that abstract classes are generally used in large projects, because I think they really have to follow too many "rules" and are inconvenient to use! Of course, this is just my opinion. For more information, the PHP abstract class application is single-inherited. that is to say, you can only inherit from one class, but not one class inherits from Class A and B, if you want to implement such a function, you have to use the interface-related knowledge. I will not discuss the PHP interface knowledge here! In a single sentence, multiple interfaces are inherited!

Bytes. The same is true in PHP. we...

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.