PHP abstract methods and examples of abstract class usage

Source: Internet
Author: User
In the OOP language, a class can have one or more subclasses, and each class has at least one public method that accesses its interface as an external code. and abstract methods are introduced for the convenience of inheritance, let us first look at the definition of abstract and abstract methods to illustrate its purpose.

abstract methods and abstract classes
In the OOP language, a class can have one or more subclasses, and each class has at least one public method as
The external code accesses its interface. and abstract method is to facilitate the introduction of inheritance, we first look at the abstract class and
The definition of an abstract method illustrates its purpose.
What is an abstract method? The method that we define in the class without a method is an abstract method, the so-called no-side
The law refers to the absence of curly braces and the contents of the method declaration, but directly after the method name at the time of declaration.
With the end of the semicolon, plus a keyword "abstract" when declaring an abstract method;
For example:
Abstract function fun1 ();
Abstract function fun2 ();
The above example is the abstract method that "abstract" modifies without the method body "fun1 ()" and "fun2 ()", do not forget
There is also a semicolon behind the abstract method, so what is an abstract class? As long as there is one method within a class that is abstract
method, the class is defined as an abstract class, and the abstract class is also modified using the "abstract" keyword; in abstract class
Polygons can have methods and member properties that are not abstract, but as long as one method is an abstract method, the class must declare
For abstract classes, use "abstract" to decorate.
For example:
Code Snippets

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

The above example defines an abstract class "Demo" that uses "abstract" to modify, in which the class defines a
A member property "$test", and two abstract methods "Fun1" and "fun2" have a non-abstract method fun3 ();
How do we use abstract classes? The most important point is that an abstract class cannot produce an instance object, so it cannot directly make
We have mentioned many times before that classes cannot be used directly, and we use objects that are instantiated by class
The image class cannot produce an instance object. What is the use of abstract classes? We are the modulo that the abstract method is to be overloaded as a subclass
Board, the definition of an abstract class is equivalent to defining a specification that requires subclasses to obey, subclass-Class relay abstraction
class, the abstract method inside the abstract class is implemented according to the needs of the subclass. Subclasses must have all the abstract methods in the parent class
are implemented, otherwise there is an abstract method in the subclass, then the subclass is an abstract class, or it cannot be instantiated. Why am I
Do we have to inherit from the abstract class? Because sometimes we have to implement some functions that must inherit from the abstract class, otherwise
These functions you can not realize, if inherit the abstract class, it is necessary to implement the abstract method of the class;
Code Snippets

<?php abstract class demo{var $test, abstract function fun1 (), abstract function fun2 (), function fun3 () {...}} $dem O=new Demo (); The abstract class is capable of producing an instance object, so it is wrong to instantiate the object to the subclass class Test extends demo{function fun1 () {...} function fun2 () {...}} $test =new Test (); Subclasses can instantiate an object because all abstract methods in the parent class are implemented?>

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.