In-depth interpretation of abstract and abstract classes in PHP

Source: Internet
Author: User

In an object-oriented (OOP) language, a class can have one or more subclasses, and each class has at least one public method that is accessed as an interface for external code. abstract methods are introduced to facilitate inheritance , and now look at how abstract and abstract methods are defined and their characteristics.


What is an abstract method?

The method that we define in our class is the abstract method , without the method body, the so-called no method body is in the method declaration without curly braces and the contents of it, but directly declared when the method name after the end of the semicolon, in addition to the declaration of the abstract method is added a keyword " abstract"to modify.


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.

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

Such as:

#抽象类abstract class class1{#抽象方法 abstract function fun1 (); ......}


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

 #定义抽象类abstract  class User{          #定义抽象方法     abstract protected function getuser ();      #普通方法     public function print_content () {              #调用抽象方法         print   $this->getuser ()     }} #继承抽象类class  vipUser extends User{               #重写抽象方法           protected function getuser () {               return  "abstract class and abstract method Http://blog.51cto.com";         }} #实例化子类 $user  = new vipuser ();  #调用基类普通方法 $user->print_content (); 

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 and above, 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.

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 (+); echo "You Sal is http://blog.51cto.com". Vipuser::getsal ();

This article is from the "13481024" blog, please be sure to keep this source http://13491024.blog.51cto.com/13481024/1983881

In-depth interpretation of abstract and abstract classes in PHP

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.