php PHP中的抽象类是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2558559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is an abstract class in PHP?
提问by udaya
What is an abstract class in PHP?
PHP中的抽象类是什么?
How can it be used?
如何使用?
回答by selfawaresoup
An abstract class is a class that contains at least one abstract method, which is a method without any actual code in it, just the name and the parameters, and that has been marked as "abstract".
抽象类是至少包含一个抽象方法的类,该方法中没有任何实际代码,只有名称和参数,并且已被标记为“抽象”。
The purpose of this is to provide a kind of template to inherit from and to force the inheriting class to implement the abstract methods.
这样做的目的是提供一种模板来继承并强制继承类实现抽象方法。
An abstract class thus is something between a regular class and a pure interface. Also interfaces are a special case of abstract classes where ALL methods are abstract.
因此,抽象类介于常规类和纯接口之间。接口也是抽象类的一种特殊情况,其中所有方法都是抽象的。
See this sectionof the PHP manual for further reference.
如需进一步参考,请参阅PHP 手册的这一部分。
回答by Dhairya Lakhera
Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.
抽象类是包含一个或多个抽象方法的类。抽象方法是已声明但不包含实现的方法。抽象类不能被实例化,并且需要子类为抽象方法提供实现。
1. Can not instantiate abstract class: Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract.
1.不能实例化抽象类:定义为抽象的类不能实例化,任何包含至少一个抽象方法的类也必须是抽象的。
Example below :
下面的例子:
abstract class AbstractClass
{
abstract protected function getValue();
abstract protected function prefixValue($prefix);
public function printOut() {
echo "Hello how are you?";
}
}
$obj=new AbstractClass();
$obj->printOut();
//Fatal error: Cannot instantiate abstract class AbstractClass
2. Any class that contains at least one abstract method must also be abstract: Abstract class can have abstract and non-abstract methods, but it must contain at least one abstract method. If a class has at least one abstract method, then the class must be declared abstract.
2.任何包含至少一个抽象方法的类也必须是抽象的:抽象类可以有抽象和非抽象方法,但必须至少包含一个抽象方法。如果一个类至少有一个抽象方法,那么这个类必须被声明为抽象的。
Note: Traits support the use of abstract methods in order to impose requirements upon the exhibiting class.
注意:Traits 支持使用抽象方法,以便对展示类施加要求。
Example below :
下面的例子:
class Non_Abstract_Class
{
abstract protected function getValue();
public function printOut() {
echo "Hello how are you?";
}
}
$obj=new Non_Abstract_Class();
$obj->printOut();
//Fatal error: Class Non_Abstract_Class contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Non_Abstract_Class::getValue)
3. An abstract method can not contain body: Methods defined as abstract simply declare the method's signature - they cannot define the implementation. But a non-abstract method can define the implementation.
3. 抽象方法不能包含主体:定义为抽象的方法只是声明方法的签名——它们不能定义实现。但是非抽象方法可以定义实现。
abstract class AbstractClass
{
abstract protected function getValue(){
return "Hello how are you?";
}
public function printOut() {
echo $this->getValue() . "\n";
}
}
class ConcreteClass1 extends AbstractClass
{
protected function getValue() {
return "ConcreteClass1";
}
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass1";
}
}
$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."\n";
//Fatal error: Abstract function AbstractClass::getValue() cannot contain body
4. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child:If you inherit an abstract class you have to provide implementations to all the abstract methods in it.
4.从抽象类继承时,父类声明中标记为抽象的所有方法必须由子类定义:如果继承抽象类,则必须为其中的所有抽象方法提供实现。
abstract class AbstractClass
{
// Force Extending class to define this method
abstract protected function getValue();
// Common method
public function printOut() {
print $this->getValue() . "<br/>";
}
}
class ConcreteClass1 extends AbstractClass
{
public function printOut() {
echo "dhairya";
}
}
$class1 = new ConcreteClass1;
$class1->printOut();
//Fatal error: Class ConcreteClass1 contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (AbstractClass::getValue)
5. Same (or a less restricted) visibility:When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
5.相同(或较少限制)的可见性:从抽象类继承时,父类声明中标记为抽象的所有方法必须由子类定义;此外,这些方法必须定义为具有相同(或较少限制)的可见性。例如,如果抽象方法定义为受保护,则函数实现必须定义为受保护或公共,但不能是私有的。
Note that abstract method should not be private.
注意抽象方法不应该是私有的。
abstract class AbstractClass
{
abstract public function getValue();
abstract protected function prefixValue($prefix);
public function printOut() {
print $this->getValue();
}
}
class ConcreteClass1 extends AbstractClass
{
protected function getValue() {
return "ConcreteClass1";
}
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass1";
}
}
$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."<br/>";
//Fatal error: Access level to ConcreteClass1::getValue() must be public (as in class AbstractClass)
6. Signatures of the abstract methods must match:When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child;the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. For example, if the child class defines an optional argument, where the abstract method's signature does not, there is no conflict in the signature.
6.抽象方法的签名必须匹配:从抽象类继承时,父类声明中所有标记为抽象的方法必须由子类定义;方法的签名必须匹配,即类型提示和所需的数量参数必须相同。例如,如果子类定义了一个可选参数,而抽象方法的签名没有,则签名不存在冲突。
abstract class AbstractClass
{
abstract protected function prefixName($name);
}
class ConcreteClass extends AbstractClass
{
public function prefixName($name, $separator = ".") {
if ($name == "Pacman") {
$prefix = "Mr";
} elseif ($name == "Pacwoman") {
$prefix = "Mrs";
} else {
$prefix = "";
}
return "{$prefix}{$separator} {$name}";
}
}
$class = new ConcreteClass;
echo $class->prefixName("Pacman"), "<br/>";
echo $class->prefixName("Pacwoman"), "<br/>";
//output: Mr. Pacman
// Mrs. Pacwoman
7. Abstract class doesn't support multiple inheritance:Abstract class can extends another abstract class,Abstract class can provide the implementation of interface.But it doesn't support multiple inheritance.
7.抽象类不支持多重继承:抽象类可以扩展另一个抽象类,抽象类可以提供接口的实现,但不支持多重继承。
interface MyInterface{
public function foo();
public function bar();
}
abstract class MyAbstract1{
abstract public function baz();
}
abstract class MyAbstract2 extends MyAbstract1 implements MyInterface{
public function foo(){ echo "foo"; }
public function bar(){ echo "bar"; }
public function baz(){ echo "baz"; }
}
class MyClass extends MyAbstract2{
}
$obj=new MyClass;
$obj->foo();
$obj->bar();
$obj->baz();
//output: foobarbaz
Note: Please note order or positioning of the classes in your code can affect the interpreter and can cause a Fatal error. So, when using multiple levels of abstraction, be careful of the positioning of the classes within the source code.
注意:请注意代码中类的顺序或位置会影响解释器并可能导致致命错误。因此,在使用多级抽象时,请注意源代码中类的定位。
below example will cause Fatal error: Class 'horse' not found
下面的示例将导致致命错误:找不到“马”类
class cart extends horse {
public function get_breed() { return "Wood"; }
}
abstract class horse extends animal {
public function get_breed() { return "Jersey"; }
}
abstract class animal {
public abstract function get_breed();
}
$cart = new cart();
print($cart->get_breed());
回答by Sarfraz
An abstract class is a class that is only partially implemented by the programmer. It may contain one or more abstract methods. An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class.
抽象类是程序员仅部分实现的类。它可能包含一个或多个抽象方法。抽象方法只是一个函数定义,用于告诉程序员该方法必须在子类中实现。
There is good explanation of that here.
还有的是很好的解释在这里。
回答by vivek s vamja
Abstract Class
1. Contains an abstract method
2. Cannot be directly initialized
3. Cannot create an object of abstract class
4. Only used for inheritance purposes
抽象类
1. 包含抽象方法
2. 不能直接初始化
3. 不能创建抽象类的对象
4. 仅用于继承目的
Abstract Method
1. Cannot contain a body
2. Cannot be defined as private
3. Child classes must define the methods declared in abstract class
抽象方法
1. 不能包含主体
2. 不能定义为私有
3. 子类必须定义抽象类中声明的方法
Example Code:
示例代码:
abstract class A {
public function test1() {
echo 'Hello World';
}
abstract protected function f1();
abstract public function f2();
protected function test2(){
echo 'Hello World test';
}
}
class B extends A {
public $a = 'India';
public function f1() {
echo "F1 Method Call";
}
public function f2() {
echo "F2 Method Call";
}
}
$b = new B();
echo $b->test1() . "<br/>";
echo $b->a . "<br/>";
echo $b->test2() . "<br/>";
echo $b->f1() . "<br/>";
echo $b->f2() . "<br/>";
Output:
输出:
Hello World
India
Hello World test
F1 Method Call
F2 Method Call
回答by Elangovan
- Abstract Class contains only declare the method's signature, they can't define the implementation.
- Abstraction class are defined using the keyword abstract.
- Abstract Class is not possible to implement multiple inheritance.
- Latest version of PHP 5 has introduces abstract classes and methods.
- Classes defined as abstract , we are unable to create the object ( may not instantiated )
- 抽象类只包含声明方法的签名,不能定义实现。
- 抽象类是使用关键字abstract定义的。
- 抽象类不可能实现多重继承。
- 最新版本的 PHP 5 引入了抽象类和方法。
- 定义为抽象的类,我们无法创建对象(可能未实例化)
回答by Ankur Kumar Singh
Abstract Classes are those classes which can not be initialised directly. Or in other word we can say that abstract classes are those classes which object can not be created directly. In PHP abstract classes are defied with keyword abstract.
抽象类是那些不能直接初始化的类。或者换句话说,我们可以说抽象类是那些不能直接创建对象的类。在 PHP 中,抽象类是用关键字abstract 定义的。
Also to become one class abstract ateast one method of the class must be abstract.
同样要成为一个抽象类,该类的一个方法必须是抽象的。
For the detail of the abstract class you can refer to my blog on Abstract Class in PHP.
有关抽象类的详细信息,您可以参考我关于PHP中的抽象类的博客。
回答by shriyash deshmukh
An abstract class is like the normal class it contains variables it contains protected variables functions it contains constructor only one thing is different it contains abstract method.
抽象类就像普通类一样,它包含变量,它包含受保护的变量,函数,它包含构造函数,只有一件事不同,它包含抽象方法。
The abstract method means an empty method without definition so only one difference in abstract class we can not create an object of abstract class
抽象方法意味着没有定义的空方法,所以抽象类只有一个区别我们不能创建抽象类的对象
Abstract must contains the abstract method and those methods must be defined in its inheriting class.
Abstract 必须包含抽象方法,并且这些方法必须在其继承类中定义。

