Analysis on abstract classes and abstract methods in php _ PHP Tutorial

Source: Internet
Author: User
Analysis of abstract classes and abstract methods in php. Let's take a look at the analysis of abstract classes and abstract methods in php. I hope this article will be helpful to you. In the object-oriented language (OOP), let's take a look at the analysis of abstract classes and abstract methods in php. I hope this article will be helpful to you.

In object-oriented language (OOP), a class can have one or more subclasses, and each class has at least one public method as an interface for external code access. Abstract methods are introduced to facilitate Inheritance. now let's take a look at how abstract classes and abstract methods are defined and their characteristics.

What is an abstract method? The method defined in the class only has the method name and does not have the method body. The so-called no method body means that there is no braces and content in the method declaration, instead, add a semicolon to the end of the method name and add the keyword "abstract" to declare the abstract method.


1. abstract Keywords: abstract

Abstraction cannot be specified, but there are certain concepts or names. to declare an abstract class or method in PHP, we need to use the adstract keyword.

2. definition of abstract methods and abstract classes

At least one method in a class is abstract, which is called an abstract class. Therefore, if an abstract class is defined, the abstract method is first defined.

Abstract class class1 {

Abstract function fun1 ();
......
}

1. there must be at least one abstract method in the class.
2. abstract methods cannot have {}
3. abstract must be added before the abstract method.


3. abstract class and method usage rules


Abstract classes have the following features:

1. it cannot be instantiated and can only be inherited

2. all abstract methods must be reloaded in the inherited derived classes before they can be instantiated.

The abstract method declaration is as follows:

The code is as follows:

Abstract function fun1 ();

?>


What is an abstract class? As long as a method in a class is an abstract method, this class must be defined as an abstract class. Abstract classes also need to be modified with the keyword "abstract". abstract classes cannot instantiate objects. Therefore, abstract methods are used as templates that overload subclass methods, and methods in inherited abstract classes must be implemented.

Examples of the implementation of abstract classes and abstract classes are as follows:

The code is as follows:

Abstract class User {// define an abstract class
Abstract protected function getUser (); // defines the abstract method
Public function print_content (){
Print $ this-> getUser ();
}
}

Class vipUser extends User {
Protected function getUser (){
Return "abstract classes and abstract methods ";
}
}

$ User = new vipUser (); // instantiate a subclass
$ User-> print_content (); // abstract class and abstract method
?>


Note: When an abstract class inherits another abstract class (the purpose is to extend the abstract class), the abstract method of the parent class cannot be overwritten.

In PHP5.1, abstract classes support static abstract methods. The following example shows that static abstract methods can be declared. This method must be a static method.

The code is as follows:

Abstract class User {
Protected static $ sal = 0;
Static abstract function getSal ();
Static abstract function setSal ($ sal );
}
Class VipUser extends User {
Static function getSal (){
Return self: $ sal;
}
Static function setSal ($ sal ){
Self: $ sal = $ sal;
}
}
VipUser: setSal (100 );
Echo "you sal is". VipUser: getSal ();
?>

I hope this article will be helpful to you. In the object-oriented (OOP) language ,...

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.