Explain the abstract methods and abstract classes in php

Source: Internet
Author: User
This article describes the abstract methods and abstract classes in php and provides an in-depth understanding of abstract methods and abstract classes. 1. what is an abstract method?

The method we define in the class is the abstract method. The so-called "no method body" means that there is no braces or content in the statement. Instead, a semicolon is directly added after the method name during the declaration, in addition, a keyword "abstract" must be added to the method when declaring an abstract method.

For example:
Abstract function fun1 ();
Abstract function fun2 ();

2. what is an abstract class?

As long as a method in a class is an abstract method, this class is defined as an abstract class, and the abstract class should also be modified using the "abstract" keyword; abstract classes can contain non-abstract methods and member attributes. However, if a method is an abstract method, the class must be declared as an abstract class and be modified using abstract.

For example:

abstract class demo{var $test;abstract function fun1();abstract function fun2();}

3. What is the role of an abstract class?

In the preceding example, an abstract class "demo" is defined and "abstract" is used for modification. in this class, a member attribute "$ test" is defined ", and two abstract methods "fun1" and "fun2" have a non-abstract method fun3 (). how can we use abstract classes? The most important thing is that abstract classes cannot generate instance objects, so they cannot be used directly. we have mentioned many times that classes cannot be used directly. We use objects instantiated by classes, so what is the purpose of declaring an abstract class when the image extraction class cannot generate instance objects? The abstract method is used as a module for subclass overloading. defining an abstract class is equivalent to defining a specification that requires sub-classes to comply with. after the sub-classes follow the function abstraction class, implement the abstract methods in the abstract class according to the requirements of the subclass. The subclass must implement all the abstract methods in the parent class. Otherwise, there are still abstract methods in the subclass, so the subclass or abstract class still cannot be instantiated. why do we have to inherit from the abstract class? Sometimes, to implement some functions, we must inherit from the abstract class; otherwise, you cannot implement these functions. If the abstract class is inherited, we must implement the abstract methods in the class;

For example:

Abstract class mode {var $ test; abstract function fun1 (); abstract function fun2 (); function fun2 (){....}} $ mode = new mode (); // The abstract class can only be used to generate instance objects. Therefore, this is an error. The instantiated object is handed over to the subclass class test extends mode {function fun1 () {....} function fun2 (){....}} $ test = new test (); // The subclass can instantiate the object, because the abstract method of all parent classes is implemented.

The preceding section describes the abstract methods and abstract classes in php. For more information, see other related articles in the first PHP community!

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.