PHP调用内部类parents方法而不扩展parent


PHP call inner class parents method without extending parent

是否可以调用未扩展到其父类的子类中的parents方法?我有这样的东西:

<?php
class wrapper {
    private $inner;
    public function __construct() {
        $this->inner = new inner();
    }
    // this will never get called :(
    public function foo() {
        echo "foo called in parent";
    }
    public function bar() {
        return $this->inner->bar();
    }
    public function getOtherStuff() {
        return $this->inner->blafasel;
    }
}
class inner { // would only be created in class wrapper, never outside
    public $blafasel;
    public function __construct() {
        // do something
        $this->blafasel = "other stuff";
    }
    public function bar() {
        // do stuff
        echo "bar called in inner";
        $this->foo();  // fatal error, method not declared
        parent::foo(); // fatal error, class has no parent
        // ??
        return "something"
    }
}
$test      = new wrapper();
$something = $test->bar();
?>

请不要问我为什么不使用class inner extends wrapper。我不得不像这样使用它,因为旧东西和其他需要的东西。那么,是否可以调用wrapper::foo而不必返回static?包装器使用了一些内部和stuff的公共变量
我曾尝试在调用inner的构造函数时添加$this,但这只会导致内存溢出或只是包装器的反射。那么,是否可以在inner‘b bar((中调用包装器的foo((方法,或者我必须重写整个方法,以便使用扩展类?我知道我必须重写它,但这需要几个星期,我需要这个东西。。好昨天

感谢您的任何帮助/提示/更正:(

编辑

有更多的inner类将在wrappers构造函数中调用,它们都有(某种程度上(相同的方法,但做不同的事情,这取决于内部类本身。(整个代码太长,即使是粘贴标签(

一些片段:

class wrapper {
    public function __construct($loadclass=1) {
        if($loadstuff==1)
            $this->inner = new inner();
        else
            $this->inenr = new otherinner();
    }
}

然后

class otherinner {
    public function bar() {
        // doing different stuff than inner::bar()
        // but call wrappers foo() may with possible args
    }
}

我希望这将澄清"为什么不使用extends">

class Inner extends Wrapper {
    public function __construct() {
         parent::foo();
    }
}
new Inner();

您当前的包装器期望注入内部对象,该包装器现在是内部将完成或扩展的部分对象

通过扩展分部类,扩展类继承分部类。没有扩展,就没有"父级"。

您只是将包装器用作代码中的依赖项注入,而不是扩展。