PHP Abstract class inherits abstract class UML abstract class abstract class example

Source: Internet
Author: User
For PHP programmers, the most difficult point to grasp is the PHP abstract class application of this piece of knowledge. As a novice is not yet to use object-oriented knowledge to programming, but later development, using classes to encapsulate or use the interface, such as the application of a variety of modular development, which is of course inevitable.

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. Then in PHP it is the same, we abstract a class, we can indicate the general behavior of the class, this class should be a template, it indicates that its child methods must implement some of the behavior.

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, not able to 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. Inherits the subclass of the abstract class, and if all abstract methods are not implemented, 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.

<?PHPAbstractclassShape {AbstractprotectedfunctionGet_area (); //Unlike the general method, this method does not have curly braces//You cannot create an instance of this abstract class: $Shape _rect=NewShape ();} classRectangleextendsshape{Private$width; Private$height; function__construct ($width=0,$height=0) {$this->width=$width; $this->height=$height; }   functionGet_area () {Echo($this->width+$this->height) * *; }   }   $Shape _rect=NewRectangle (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!

The above introduces the PHP abstract class, including PHP, abstract class content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.