如何在PHP中扩展2个类? [重复]

时间:2022-08-22 21:17:46

Possible Duplicate:
Multiple Inheritance in PHP
Can I extend a class using more than 1 class in PHP?

可能重复:PHP中的多重继承我可以在PHP中使用多于1个类来扩展类吗?

Does anyone know how can I extend 2 classes from one class?

有谁知道如何从一个班级扩展2个班级?

Example: How can I add another class called classB into the following class?

示例:如何将另一个名为classB的类添加到以下类中?

<?php
class Test extends classA
{
   echo "test";
} 
?>

I tried the following code, but this is not work:

我尝试了以下代码,但这不起作用:

<?php
class Test extends classA, classB
{
   echo "test";
} 
?>

Anyone know how can I solve this issue?

任何人都知道如何解决这个问题?

3 个解决方案

#1


2  

PHP, like Java, does not support multiple inheritance.

与Java一样,PHP不支持多重继承。

Coming in PHP 5.4 will be traits which attempt to provide a solution to this problem.

进入PHP 5.4将是试图提供此问题的解决方案的特征。

In the meantime, you would be best to re-think your class design. You can implement multiple interfaces if you're after an extended API to your classes.

在此期间,您最好重新考虑您的课堂设计。如果您正在使用类的扩展API,则可以实现多个接口。

#2


1  

PHP doesn't really support multiple inheritance, but there are some (somewhat messy) ways to implement it. Check out this URL for some examples:

PHP并不真正支持多重继承,但有一些(有点混乱)的方法来实现它。查看此URL以获取一些示例:

http://www.jasny.net/articles/how-i-php-multiple-inheritance/

http://www.jasny.net/articles/how-i-php-multiple-inheritance/

#3


0  

What you're asking about is called "Multiple Inheritance". PHP doesn't support this, however you'd do well to have a read through this * thread:

您所询问的内容称为“多重继承”。 PHP不支持这一点,但是您可以通过此*线程进行读取:

Multiple Inheritance in PHP

PHP中的多重继承

#1


2  

PHP, like Java, does not support multiple inheritance.

与Java一样,PHP不支持多重继承。

Coming in PHP 5.4 will be traits which attempt to provide a solution to this problem.

进入PHP 5.4将是试图提供此问题的解决方案的特征。

In the meantime, you would be best to re-think your class design. You can implement multiple interfaces if you're after an extended API to your classes.

在此期间,您最好重新考虑您的课堂设计。如果您正在使用类的扩展API,则可以实现多个接口。

#2


1  

PHP doesn't really support multiple inheritance, but there are some (somewhat messy) ways to implement it. Check out this URL for some examples:

PHP并不真正支持多重继承,但有一些(有点混乱)的方法来实现它。查看此URL以获取一些示例:

http://www.jasny.net/articles/how-i-php-multiple-inheritance/

http://www.jasny.net/articles/how-i-php-multiple-inheritance/

#3


0  

What you're asking about is called "Multiple Inheritance". PHP doesn't support this, however you'd do well to have a read through this * thread:

您所询问的内容称为“多重继承”。 PHP不支持这一点,但是您可以通过此*线程进行读取:

Multiple Inheritance in PHP

PHP中的多重继承