PHP 在不扩展父级的情况下调用内部 class 父级方法
PHP call inner class parents method without extending parent
是否可以在子项中调用父方法 class 未扩展到其父项?我有这样的东西:
<?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 ?包装器正在使用一些 public 内部变量和东西。
我试图在调用 inner 的构造函数时添加 $this
但这只会导致内存溢出或只是包装器的反射。
那么是否可以在 inner'b bar() 中调用包装器的 foo() 方法,或者我是否必须重写整个东西以便它可以使用扩展 classes?我知道我必须重写它,但这需要数周时间,我需要这个东西……好吧……昨天。
感谢任何 help/hints/corrections :)
编辑
有更多的 inner
classes 将在包装器构造函数中调用,并且都有(某种程度上)相同的方法但做不同的事情,取决于内部 class 本身。 (整个代码太长,即使是 pastebin)
一些片段:
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
}
}
我希望这会清除 "why not using extends"
class Inner extends Wrapper {
public function __construct() {
parent::foo();
}
}
new Inner();
您当前的包装器需要内部对象的注入,该包装器现在是内部将完成或 扩展
的部分对象
通过扩展部分 class,扩展 class 继承了部分 class。没有扩展名,就没有 'parent'.
您只是将包装器用作代码中的依赖注入,而不是扩展。
是否可以在子项中调用父方法 class 未扩展到其父项?我有这样的东西:
<?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 ?包装器正在使用一些 public 内部变量和东西。
我试图在调用 inner 的构造函数时添加 $this
但这只会导致内存溢出或只是包装器的反射。
那么是否可以在 inner'b bar() 中调用包装器的 foo() 方法,或者我是否必须重写整个东西以便它可以使用扩展 classes?我知道我必须重写它,但这需要数周时间,我需要这个东西……好吧……昨天。
感谢任何 help/hints/corrections :)
编辑
有更多的 inner
classes 将在包装器构造函数中调用,并且都有(某种程度上)相同的方法但做不同的事情,取决于内部 class 本身。 (整个代码太长,即使是 pastebin)
一些片段:
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
}
}
我希望这会清除 "why not using extends"
class Inner extends Wrapper {
public function __construct() {
parent::foo();
}
}
new Inner();
您当前的包装器需要内部对象的注入,该包装器现在是内部将完成或 扩展
的部分对象通过扩展部分 class,扩展 class 继承了部分 class。没有扩展名,就没有 'parent'.
您只是将包装器用作代码中的依赖注入,而不是扩展。