简体   繁体   中英

PHP call inner class parents method without extending parent

is it possible to call a parents method in a child class witch is not extended to its parent? I have something like that:

<?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();

?>

please dont ask why i've not using class inner extends wrapper . i have to use it like this cause of old stuff and other required things. So is it possible to call wrapper::foo without beeing static? wrapper is using some public variables of inner and stuff.
I've tried to add $this when calling the constructor of inner but that results only in an memory overflow or just a reflection of wrapper. So is it possible to call wrapper's foo() method in inner'b bar() or do i have to rewrite the whole thing so it could be using extended classes? I know i have to rewrite it but it would take weeks and i need this thing.. well.. yesterday.

Thanks for any help/hints/corrections :)

EDIT

There are more of inner classes that would be called in wrappers constructor, and all have (sort of) the same methods but do different stuff, depends on the inner class itself. (whole code is too long, even for pastebin)

some snippets:

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

and then

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

i hope this will clear the "why not using extends"

class Inner extends Wrapper {

    public function __construct() {
         parent::foo();
    }

}

new Inner();

Your current wrapper expects an injection of the inner object, this wrapper is now a partial object which inner will complete or extend

By extending a partial class, the extension class inherits the partial class. Without an extension, there is no 'parent'.

You are simply using wrapper as a dependency injection in your code, not as an extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM