How PHP abstract classes define and apply rules

Source: Internet
Author: User
For friends who are trying to learn PHP (http://www.maiziedu.com/course/php/), the most difficult to understand knowledge should be a few PHP abstract class applications. As a novice is not yet to use object-oriented knowledge to programming, but later in PHP development, using classes to encapsulate or use the interface and so on, the program for a variety of modular development, which is of course inevitable.
In PHP we abstract a class to indicate the general behavior of the class, which should be a template that indicates some of the behavior that its child methods must implement.
The definition of the PHP abstract class application:
Abstract class classname{
}
PHP abstract class Application Highlights:
1. Define methods in which subclasses must fully implement all the methods in this abstraction
2. You cannot create an object from an abstract class, its meaning is to be extended
3. Abstract classes usually have abstract methods, without curly braces in the method
PHP abstract class Application Focus:
1. Abstract methods do not need to implement specific functions, subclasses 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. Methods of abstract classes can have parameters, or they can be empty
4. If the abstract method has parameters, then the implementation of the subclass must also have the same number of arguments
PHP Abstract class Application Example:
Abstract public function_name (); Note No curly braces
PHP abstract class Rules:
1. A class must be declared as an abstract class as long as it contains at least one abstract method
2. Abstract method, can not contain function body
3. Inheriting a subclass of an abstract class that implements an abstract method must have the same or lower access level to the abstract method
4. Inherit the subclass of the abstract class, and if you do not implement all the abstract methods, the subclass is also an abstract class
As a demonstration, let's implement a simple abstract class: Calculate the area of a rectangle. This rectangle can be extended from the shape class.
<? Php
Abstract class Shape {
Abstract protected function Get_area ();
Unlike the general method, this method does not have 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) * *;
}
}
$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 generally in large projects will use it, because I think it is to abide by the "rules" too much, inconvenient to use! Of course it's just my opinion. Also want to say some more, PHP abstract class application is single-inheritance, that is, you can only inherit from a class, and not a class inherits the Class A, but also inherit the class B, if you want to implement such a function, you have to use the knowledge of the interface, this is not a discussion of the PHP interface knowledge! One sentence: single-Inheritance multi-Interface!
This topic was moved by Beckham on 2016-4-18 12:28
  • 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.