简体   繁体   English

PHP扩展了一个从另一个类调用的类方法,该类方法对其进行扩展并使用parent :: method调用该方法

[英]PHP extend a class method that is called from another class which extends it and calls the method with parent::method

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. 让我们称该类为A。我的类B为覆盖一个受保护的方法。 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. C类扩展了A类,D类扩展了C类。在D类内部,我尝试覆盖的方法也在此处扩展,该方法称为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. 该方法在C类中不存在,因此将在A类中进行处理。那是我在B类中覆盖的方法,显然您不能用B扩展A并将那些更改显示在D中,因为它不会掉落与类层次结构一致。 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. 因此,如果我的类b被调用并运行,则类D被调用,它将运行A中的方法并覆盖我设置的内容。 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". 这是一个示例,正如您在A类中看到的那样,有一个对setTitle的调用,它被设置为“ Example”。在我的类中,我将其设置为“ 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". 我的班级在班级D之前被调用,因此当班级D被调用时,它返回到父级,并将标题设置回“ 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. 我想发生的是B类运行并将其设置为执行D类运行所需的操作,而不是运行该类,而是将其扩展为运行我的类。 the thing is I don't think I can make class D extend my class. 问题是我不认为我可以让D类扩展我的课程。 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 我有关于它不是静态方法的错误

From what I can understand _thefunction() does something to another object($headBlock?). 据我了解,_thefunction()对另一个对象($ headBlock?)有作用。 Your class (B) changes something in that object, then class D goes and changes it back to something else. 您的(B)类更改了该对象中的某些内容,然后D类进行了更改,并将其更改回其他内容。 Only way to fix this is to somehow disable D's intereference or have class B's method run after it. 解决此问题的唯一方法是以某种方式禁用D的inreference或在其之后运行类B的方法。

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. 您可以通过C继承B而不是A来使用继承,尽管我不知道这些对程序结构可能产生哪些副作用。

Edit: Seeing as this is Magento we are talking about, maybe you should ask specifically about Magento either in SO or the Magento forums. 编辑:既然我们正在谈论这是Magento,也许您应该在SO或Magento论坛中专门询问Magento。 There's very little you can't override in it; 您几乎无法覆盖其中的任何内容; maybe you've taken a wrong turn somewhere in your approach? 也许您在方法中的某个地方走错了路?

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. 那就是我在类B中覆盖的方法,显然您不能用B扩展A并将这些更改显示在D中,因为它不符合类层次结构。 I might be wrong, so correct me please. 我可能错了,所以请纠正我。

I'm afraid you're right here. 恐怕你就在这里。 If you don't rework the whole concept there's not much you can do. 如果您不重新设计整个概念,那么您将无能为力。

I tried to disable the interference by having it call myclass::thefunction but of course I got an error saying something about it not being a static method. 我试图通过使它调用myclass :: thefunction来禁用干扰,但是我当然收到一个错误,说明它不是静态方法。

Are you able to modify class D? 您可以修改D类吗? I don't understand this part. 我不明白这部分。 If so, you wouldn't be asking this question in the first place, I presume. 如果是这样,我想您根本不会问这个问题。

Maybe some specifics about Magento could help? 也许有关Magento的一些细节可能有所帮助? There's probably some other way to achieve the desired outcome. 可能还有其他方法可以达到期望的结果。

What you are trying to do probably can't be done for at least of two reasons: 您尝试做的事情可能由于至少两个原因而无法完成:

  1. Method _thefunction() is protected in B so it is accessible only for B and its descendants. 方法_thefunction()在B中受保护,因此仅B及其后代可以访问。
  2. You can't call method of object B for object of class D because D is not neither B nor descendant of B. 您不能为类D的对象调用对象B的方法,因为D既不是B也不是B的后代。

You are trying to call B::_theFunciton() but the method in B is protected (meaning that all access to that method is reserved for its own class or any subclasses, which D is not, the class hierarchy for D is... A->C->D, no B in there.) 您正在尝试调用B :: _ theFunciton(),但是B中的方法受到保护(这意味着对该方法的所有访问都保留给它自己的类或任何子类使用,而D不是,D的类层次结构是... A-> C-> D,那里没有B。)

The function is also not static, so you cant reference it like B::blah() 该函数也不是静态的,因此您不能像B :: blah()那样引用它

If you really want this then you could define _theFunction as public static, however if you really think that is an object related method then the only good practice way is to be a subclass from it. 如果您确实希望这样做,则可以将_theFunction定义为公共静态对象,但是,如果您确实认为这是一个与对象相关的方法,则唯一的好习惯方法是将其作为子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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