doufei2007 2010-04-21 21:12
浏览 31
已采纳

PHP扩展了一个类方法,该方法从另一个类调用,该类扩展它并使用parent ::方法调用该方法

I am trying to extend a class and override one of its methods. lets call that class A. My class, class B, is overiding a protected method. Class C extends class A and Class D extends class C. Inside of Class D, the method I am trying to overwrite is also extended here and that calls parent::mymethodimoverriding. That method does not exist in class C so it goes to it in class A. That is the method I am overiding in class B and obviously you can't extend A with B and have those changes show up in D since it does not fall in line with the class hierarchy. I might be wrong, so correct me please.

so if my class b is called and ran then class D gets called it runs the method in A and overwrites what I had set. I am thinking there must be a way to get this to work, I am just missing something.

Here is an example, as you can see in class A there is a call to setTitle and it is set to "Example" In my class I set it to "NewExample". My class is getting called before class D so when class D is called it goes back to the parent and sets the title back to "Example". What I would like to happen is class B runs and sets it to do what I need it to then class D run and instead of it hitting the class I am extending for it to hit my class instead. the thing is I don't think I can make class D extend my class. so now it is hitting the class I am extending

class A{
  protected function _thefunction(){
    setTitle("Example");
  }
}

class B extends A{
  protected function _thefunction(){
    My new code here
    setTitle("NewExample");
  }
}

class C extends A{
  nothing that matters in here for what I am doing
}

class D extends C{
  protected function _thefunction(){
    parent::_thefunction();
    additional code here
  }
}

I tried doing

class D extends C{
  protected function _thefunction(){
    B::_thefunction();
    additional code here
  }
}

and I got errors about it not being a static method

  • 写回答

4条回答 默认 最新

  • douzhenao6515 2010-04-21 22:05
    关注

    From what I can understand _thefunction() does something to another object($headBlock?). Your class (B) changes something in that object, then class D goes and changes it back to something else. Only way to fix this is to somehow disable D's intereference or have class B's method run after it.

    You could use inheritance by having C inherit B instead of A, although I don't know what side-effects these might have to the structure of your program.

    Edit: Seeing as this is Magento we are talking about, maybe you should ask specifically about Magento either in SO or the Magento forums. There's very little you can't override in it; maybe you've taken a wrong turn somewhere in your approach?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗