Php 简明教程

PHP – Abstract Classes

PHP 中的保留字列表包括“abstract”关键字。当用“abstract”关键字定义一个类时,它不能被实例化,即,你不能声明这样的类的对象。抽象类可以由另一个类扩展。

The list of reserved words in PHP includes the "abstract" keyword. When a class is defined with the "abstract" keyword, it cannot be instantiated, i.e., you cannot declare a new object of such a class. An abstract class can be extended by another class.

abstract class myclass {
   // class body
}

如上所述,你可以 cannot declare an object of this class 。因此,下面的语句 -

As mentioned above, you cannot declare an object of this class. Hence, the following statement −

$obj = new myclass;

将导致一个 error 消息,如下所示 -

will result in an error message as shown below −

PHP Fatal error:  Uncaught Error: Cannot instantiate abstract class myclass

一个抽象类可能包含属性、常量或方法。类成员可以是 public、private 或 protected 类型的。一个类中的一个或更多方法也可以被定义为抽象的。

An abstract class may include properties, constants or methods. The class members may be of public, private or protected type. One or more methods in a class may also be defined as abstract.

如果一个类中的任何方法是抽象的,类本身必须是一个抽象类。换句话说,一个普通的类不能有在其中定义的抽象方法。

If any method in a class is abstract, the class itself must be an abstract class. In other words, a normal class cannot have an abstract method defined in it.

这会产生一个 error -

This will raise an error

class myclass {
   abstract function myabsmethod($arg1, $arg2);
   function mymethod() #this is a normal method {
      echo "Hello";
   }
}

error message 将被显示为 -

The error message will be shown as −

PHP Fatal error:  Class myclass contains 1 abstract method
and must therefore be declared abstract

你可以使用一个抽象类作为一个父类并用一个子类扩展它。但是,子类必须为父类中的每一个抽象方法提供具体的实现,否则会遇到错误。

You can use an abstract class as a parent and extend it with a child class. However, the child class must provide concrete implementation of each of the abstract methods in the parent class, otherwise an error will be encountered.

Example

在以下代码中, myclass 是一个 abstract classmyabsmethod() 作为其 abstract method 。它派生的类是 mynewclass ,但它没有实现其父类中的抽象方法。

In the following code, myclass is an abstract class with myabsmethod() as an abstract method. Its derived class is mynewclass, but it doesn’t have the implementation of the abstract method in its parent.

<?php
   abstract class myclass {
      abstract function myabsmethod($arg1, $arg2);
      function mymethod() {
         echo "Hello";
      }
   }
   class newclass extends myclass {
      function newmethod() {
         echo "World";
      }
   }
   $m1 = new newclass;
   $m1->mymethod();
?>

在这种情况下 error message -

The error message in such a situation is −

PHP Fatal error:  Class newclass contains 1 abstract method and must
therefore be declared abstract or implement the remaining
methods (myclass::myabsmethod)

它表明新类应该实现抽象方法或者应该被声明为抽象类。

It indicates that newclass should either implement the abstract method or it should be declared as an abstract class.

Example

在以下 PHP 脚本中,我们有 marks 作为抽象类,其中 percent() 是其中一个抽象方法。另一个 student 类扩展了 marks 类并实现了其 percent() 方法。

In the following PHP script, we have marks as an abstract class with percent() being an abstract method in it. Another student class extends the marks class and implements its percent() method.

<?php
   abstract class marks {
      protected int $m1, $m2, $m3;
      abstract public function percent(): float;
   }

   class student extends marks {
      public function __construct($x, $y, $z) {
         $this->m1 = $x;
         $this->m2 = $y;
         $this->m3 = $z;
      }
      public function percent(): float {
         return ($this->m1+$this->m2+$this->m3)*100/300;
      }
   }

   $s1 = new student(50, 60, 70);
   echo "Percentage of marks: ". $s1->percent() . PHP_EOL;
?>

它将生成以下 output

It will produce the following output

Percentage of marks: 60

Difference between Interface and Abstract Class in PHP

PHP 中抽象类的概念与接口非常相似。但是,接口和抽象类之间有几个不同之处。

The concept of abstract class in PHP is very similar to interface. However, there are a couple of differences between an interface and an abstract class.

Abstract class

Interface

Use abstract keyword to define abstract class

Use interface keyword to define interface

Abstract class cannot be instantiated

Interface cannot be instantiated.

Abstract class may have normal and abstract methods

Interface must declare the methods with arguments and return types only and not with any body.

Abstract class is extended by child class which must implement all abstract methods

Interface must be implemented by another class, which must provide functionality of all methods in the interface.

Can have public, private or protected properties

Properties cannot be declared in interface