A detailed description of the definitions and rules of abstract classes and abstract methods in PHP

Source: Internet
Author: User
Tags define abstract
in theObject oriented (OOP) language, a class can have one or more subclasses, and each class has at least one public method interface that is accessed as an external code. and abstract method is to facilitate the introduction of inheritance, and now take a look atAbstract class and how the abstract methods are defined and their characteristics.

What is an abstract method? The method we define in our class is the abstract method, without the method body, the so-called No method is the method declaration without curly braces and the contents of it, but directly declared when the method name after the end of the semicolon, and in the declaration of the abstract method, plus a keyword "abstract" to decorate.

1. Abstract Key Words: Abstraction

Abstract is not an exact description, but there is a certain concept or name, in PHP to declare an abstract class or method we need to use the Adstract keyword.

2. Abstract methods and definitions of abstract classes

At least one of the methods in a class is abstract, which we call abstract classes. So if you define an abstract class, you first define an abstract method.

The code is as follows:

Abstract class class1{                                               abstract function fun1 ();    ......}

1. There is at least one abstract method in the class
2. The abstract method does not allow {}
3. Abstract methods must be added before


3. Abstract class and method usage rules

Several features of abstract classes:

1, can not be instantiated, can only be inherited

2. Inherit all abstract method overloads in the derived class to instantiate

The Declaration on abstract methods is as follows:

The code is as follows:

<?phpabstract function fun1 ();? >


What is an abstract class? As long as there is one method in a class that is an abstract method, the class must be defined as an abstract class. Abstract classes also use the keyword "abstract" to decorate, abstract classes can not instantiate objects, so the abstract method as a subclass method overloaded template used, and to the inherited abstract class methods are implemented.

Examples of implementations of abstract classes and abstract classes are as follows:

The code is as follows:

<?phpabstract class user{  //define abstract    protected function getUser ();//define abstract method public    function print _content () {        print $this->getuser ();}    } Class Vipuser extends user{    protected function GetUser () {        return "abstract class and abstract method Www.jb51.net";    }} $user =new Vipuser (); Instantiate subclass $user->print_content (); Abstract class and abstract method?>

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

In PHP5.1, the static abstract method is supported in the abstract class. In the following example, we see that the static abstract method can be declared. When implementing this method, it must be a static method.

The code is as follows:

<?phpabstract class user{protected static  $sal =0; static abstract function getsal (); static abstract function sets Al ($sal); }class Vipuser extends user{static function Getsal () {  return self:: $sal;} static function Setsal ($sal) {  self::$ Sal= $sal; }}vipuser::setsal (+); echo "You Sal is Www.jb51.net". Vipuser::getsal ();? >

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.