Home  >  Article  >  Backend Development  >  Analysis of issues related to abstract classes and abstract methods in PHP_PHP tutorial

Analysis of issues related to abstract classes and abstract methods in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:42:30970browse

Let’s take a look at the analysis of abstract classes and abstract methods in PHP. I hope this article will be helpful to all students.

In object-oriented (OOP) languages, a class can have one or more subclasses, and each class has at least one public method as an interface for external code to 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 we define in the class with only the method name and no method body is an abstract method. The so-called no method body means that there is no curly braces and the content in the method when it is declared. Instead, when it is declared directly, a semicolon is added after the method name. , In addition, when declaring an abstract method, add a keyword "abstract" to modify it.


1. Abstract keyword: abstract

Abstract means that it cannot be explained exactly, but it has a certain concept or name. To declare an abstract class or method in PHP, we need to use the abstract keyword.

2. Definition of abstract methods and abstract classes

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

abstract class class1{                                                                  

abstract function fun1();


}

1. There is at least one abstract method in the class

2. Abstract methods are not allowed to contain { }
3. Abstract
must be added before the abstract method

3. Rules for using abstract classes and methods

Several characteristics of abstract classes:

1. It cannot be instantiated, but can only be inherited

2. In inherited derived classes, all abstract methods must be overloaded before they can be instantiated

Analysis of issues related to abstract classes and abstract methods in PHP_PHP tutorial

The statement about abstract methods is as follows:

The code is as follows Copy code
 代码如下 复制代码

abstract function fun1();

?>

abstract function fun1();


?>

What is an abstract class? As long as a method in a class is an abstract method, then the class must be defined as an abstract class. Abstract classes must also be modified with the keyword "abstract". Abstract classes cannot instantiate objects, so abstract methods are used as templates for subclass method overloading, and all methods in the inherited abstract class must be implemented.
 代码如下 复制代码

abstract class User{  //定义抽象类
    abstract protected function getUser(); //定义抽象方法
    public function print_content(){
        print $this->getUser();
    }
}

class vipUser extends User{
    protected function getUser(){
        return "抽象类与抽象方法";
    }
}

$user=new vipUser(); //实例化子类
$user->print_content(); //抽象类与抽象方法
?>

Abstract classes and implementation examples of abstract classes are as follows:
The code is as follows Copy code
abstract class User{ //Define abstract class 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"; } } $user=new vipUser(); //Instantiate 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), it cannot override the abstract method of the parent class.

In PHP5.1, static abstract methods are supported in abstract classes. In the example below, we see that static abstract methods can be declared. When implementing this method, it 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();
?>

Copy code
abstract class User{

protected static $sal=0;

static abstract function getSal(); } 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();
?>
http://www.bkjia.com/PHPjc/634549.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/634549.htmlTechArticleLet’s take a look at the analysis of abstract classes and abstract methods in php. I hope this article It will be helpful to all classmates. In object-oriented (OOP) languages,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn