PHP Abstract class Introduction _php skills

Source: Internet
Author: User
Tags inheritance
In natural language, we understand that the concept of abstraction is a large description of an object, which is a common characteristic of a class of objects. So is the same in PHP, where we abstract a class to indicate the general behavior of a class, which should be a template that indicates some behavior that its child methods must implement.
PHP abstract class application definition:
Abstract class classname{
}

PHP Abstract class Application essentials:
1. Define methods, subclasses must fully implement all the methods in this abstraction
2. You cannot create an object from an abstract class, it is meant to be extended
3. Abstract classes usually have an abstract method without curly braces

PHP abstract class Application Focus:
1. Abstract methods do not need to implement specific functions, subclass to complete
2. When a subclass implements a method of an abstract class, the visibility of its subclasses must be greater than or equal to the definition of the abstract method
3. Abstract class methods can have parameters, or can be empty
4. If the abstract method has parameters, the implementation of the subclass must also have the same number of parameters

PHP Abstract class Application Example:
Abstract public function_name (); Note that there are no curly braces
PHP abstract class rule:
A class must be declared as an abstract class as long as it contains at least one abstract method
Abstract method that cannot contain a function body
Inherits the subclass of an abstract class and implements an abstract method that must have the same or lower access level as the abstract method
Inherits the subclass of an abstract class, and if you do not implement all of the abstract methods, the subclass is also an abstract class
As a demo, let's implement a simple abstract class: compute the area of a rectangle. This rectangle can be extended from the shape class.
Copy Code code as follows:

<? Php
Abstract class Shape {
Abstract protected function Get_area ();
Unlike the general approach, this method has no curly 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 a simple example, basically can explain the use of abstract classes in PHP, the others do not want to say more. I think the abstract class is generally used in large projects, because I think it is really to comply with the "rules" too much, not easy to use! Of course that's just my opinion. I'd like to say something more, PHP abstract class application is a single inheritance, that is, you can only inherit from a class, and not a class inherits a class, and inherit Class B, if you want to implement such a function, you have to use the knowledge of the interface, this is not to discuss the knowledge of PHP interface! In one word: Single Inheritance multiple interfaces!

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.