你能在一堂课中扩展两门课程吗? [重复]

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

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

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

I have a class that has several child classes, however I am now adding another class which I also want to be a child of the parent, however I would also like to utilize many of the functions from one of the other child classes.

我有一个有几个子类的类,但是我现在正在添加另一个类,我也想成为父类的子类,但是我也希望使用其他子类中的许多函数。

I've thought of just moving the respective functions from the other child class to the parent, however didn't think this was really needed as it would only be these two child classes that make use of them so was hoping that I could extend from the main parent class and from one of the existing child classes.

我想过只是将相应的函数从另一个子类移动到父类,但是并不认为这是真正需要的,因为只有这两个子类才能使用它们所以希望我可以从主要的父类和一个现有的子类。

5 个解决方案

#1


13  

You can extend the child class to inherit both its parent and the child's functions, which I think is what you are trying to do.

您可以扩展子类以继承其父级和子级函数,我认为这是您要执行的操作。

class Parent
{
    protected function _doStuff();
}

class Child extends Parent
{
    protected function _doChildStuff();
}

class Your_Class extends Child
{
    // Access to all of Parent and all of Child's members
}

// Your_Class has access to both _doStuff() and _doChildStuff() by inheritance

#2


8  

No php is single inheritance language. If you can you can lookup upcoming feature in php 5.4, that is traits.

没有php是单继承语言。如果可以,你可以在php 5.4中查找即将推出的功能,这就是特性。

#3


8  

As said @morphles, this feature will be available as traits (like mixins in other languages) in php 5.4. However, if it's really needed, you can use such workaround:

正如所说的@morphles,这个功能将在php 5.4中作为特征(如其他语言中的mixins)提供。但是,如果确实需要,您可以使用以下解决方法:

// your class #1
class A {
    function smthA() { echo 'A'; }
}

// your class #2
class B {
    function smthB() { echo 'B'; }
}

// composer class
class ComposeAB {
    // list of implemented classes
    private $classes = array('A', 'B');
    // storage for objects of classes
    private $objects = array();

    // creating all objects
    function __construct() {
        foreach($this->classes as $className)
            $this->objects[] = new $className;
    }

    // looking for class method in all the objects
    function __call($method, $args) {
        foreach($this->objects as $object) {
            $callback = array($object, $method);
            if(is_callable($callback))
                return call_user_func_array($callback, $args);
        }
    }
}

$ab = new ComposeAB;
$ab->smthA();
$ab->smthB();

#4


2  

As a yes/no answer i can say no. You can't extend from multiple classes in php. but you can use interface instead.

作为是/否答案我可以说不。你不能从php中的多个类扩展。但你可以使用界面代替。

#5


0  

You can do this by thinking backwards, however it depends on the situation. If you would like to keep your classes separate, nobody can give you a good answer of how to structure your objects and inheritance unless you give us more information. Also keep in mind that getting too worried about getting class structure right is a burden in small projects and you may as well just move the methods over and be done with it and learn more about classes later.

你可以通过向后思考来做到这一点,但这取决于具体情况。如果您希望将课程分开,除非您向我们提供更多信息,否则没有人能为您提供如何构建对象和继承的良好答案。另外请记住,过于担心让类结构正确是小项目的负担,你也可以只是将方法移动并完成它,并在以后学习更多关于类的知识。

#1


13  

You can extend the child class to inherit both its parent and the child's functions, which I think is what you are trying to do.

您可以扩展子类以继承其父级和子级函数,我认为这是您要执行的操作。

class Parent
{
    protected function _doStuff();
}

class Child extends Parent
{
    protected function _doChildStuff();
}

class Your_Class extends Child
{
    // Access to all of Parent and all of Child's members
}

// Your_Class has access to both _doStuff() and _doChildStuff() by inheritance

#2


8  

No php is single inheritance language. If you can you can lookup upcoming feature in php 5.4, that is traits.

没有php是单继承语言。如果可以,你可以在php 5.4中查找即将推出的功能,这就是特性。

#3


8  

As said @morphles, this feature will be available as traits (like mixins in other languages) in php 5.4. However, if it's really needed, you can use such workaround:

正如所说的@morphles,这个功能将在php 5.4中作为特征(如其他语言中的mixins)提供。但是,如果确实需要,您可以使用以下解决方法:

// your class #1
class A {
    function smthA() { echo 'A'; }
}

// your class #2
class B {
    function smthB() { echo 'B'; }
}

// composer class
class ComposeAB {
    // list of implemented classes
    private $classes = array('A', 'B');
    // storage for objects of classes
    private $objects = array();

    // creating all objects
    function __construct() {
        foreach($this->classes as $className)
            $this->objects[] = new $className;
    }

    // looking for class method in all the objects
    function __call($method, $args) {
        foreach($this->objects as $object) {
            $callback = array($object, $method);
            if(is_callable($callback))
                return call_user_func_array($callback, $args);
        }
    }
}

$ab = new ComposeAB;
$ab->smthA();
$ab->smthB();

#4


2  

As a yes/no answer i can say no. You can't extend from multiple classes in php. but you can use interface instead.

作为是/否答案我可以说不。你不能从php中的多个类扩展。但你可以使用界面代替。

#5


0  

You can do this by thinking backwards, however it depends on the situation. If you would like to keep your classes separate, nobody can give you a good answer of how to structure your objects and inheritance unless you give us more information. Also keep in mind that getting too worried about getting class structure right is a burden in small projects and you may as well just move the methods over and be done with it and learn more about classes later.

你可以通过向后思考来做到这一点,但这取决于具体情况。如果您希望将课程分开,除非您向我们提供更多信息,否则没有人能为您提供如何构建对象和继承的良好答案。另外请记住,过于担心让类结构正确是小项目的负担,你也可以只是将方法移动并完成它,并在以后学习更多关于类的知识。