PHP class extension - 跳过中间 class 通过调用 parent 的 parent
PHP class extension - skipping the middle class by calling the parent of the parent
我目前正在尝试扩展 Webshop CMS,但 运行 遇到了问题。我有以下 classes :
class a {
public function doStuff(){
// doing some A stuff
return $something;
}
}
class b extends a {
public function doStuff(){
// doing some B Stuff
$something = parent::doStuff();
return $something;
}
}
class c extends b {
public function doStuff(){
// doing some C stuff
$something = parent::doStuff(); // <= problem here
return $something;
}
}
我无法更改 class a 或 b,因为它们是 cms (prestashop) 的核心 classes,这就是为什么我的所有代码都进入 class c。
我需要 doing some A stuff
,没有 运行 doing some B stuff
来自 class b。
我无法直接扩展 a
,因为 b
中还有我需要的其他代码。
除了 copy/pasting 来自 a->doStuff()
的所有内容并且根本不调用 parent::doStuff()
之外,我似乎无法想出一个干净的解决方案来解决这个问题。
据我所知,没有 parent::parent:: 构造或类似的东西。
有谁知道无需 copy/paste 一切的更好解决方案?
class c extends b {
public function doStuff(){
// doing some C stuff
$something = a::doStuff(); // change here parent to 'a'
return $something;
}
}
我已经使用此代码进行了测试并且它有效
<?php
class a {
public function doStuff(){
echo 'A-Stuff<pre>';
return $something;
}
}
class b extends a {
public function doStuff(){
echo 'B-Stuff<pre>';
parent::doStuff();
}
}
class c extends b {
public function doStuff(){
// doing some C stuff
a::doStuff();
echo 'C-Stuff<pre>';
}
}
$c = new c;
$c->doStuff();
这呼应
A-Stuff
C-Stuff
我目前正在尝试扩展 Webshop CMS,但 运行 遇到了问题。我有以下 classes :
class a {
public function doStuff(){
// doing some A stuff
return $something;
}
}
class b extends a {
public function doStuff(){
// doing some B Stuff
$something = parent::doStuff();
return $something;
}
}
class c extends b {
public function doStuff(){
// doing some C stuff
$something = parent::doStuff(); // <= problem here
return $something;
}
}
我无法更改 class a 或 b,因为它们是 cms (prestashop) 的核心 classes,这就是为什么我的所有代码都进入 class c。
我需要 doing some A stuff
,没有 运行 doing some B stuff
来自 class b。
我无法直接扩展 a
,因为 b
中还有我需要的其他代码。
除了 copy/pasting 来自 a->doStuff()
的所有内容并且根本不调用 parent::doStuff()
之外,我似乎无法想出一个干净的解决方案来解决这个问题。
据我所知,没有 parent::parent:: 构造或类似的东西。 有谁知道无需 copy/paste 一切的更好解决方案?
class c extends b {
public function doStuff(){
// doing some C stuff
$something = a::doStuff(); // change here parent to 'a'
return $something;
}
}
我已经使用此代码进行了测试并且它有效
<?php
class a {
public function doStuff(){
echo 'A-Stuff<pre>';
return $something;
}
}
class b extends a {
public function doStuff(){
echo 'B-Stuff<pre>';
parent::doStuff();
}
}
class c extends b {
public function doStuff(){
// doing some C stuff
a::doStuff();
echo 'C-Stuff<pre>';
}
}
$c = new c;
$c->doStuff();
这呼应
A-Stuff
C-Stuff