Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
1.7k views
in Plugins by
edited by
I remember someone ask this question few days ago but no one answered and while developing one plugin I realized the sme issue.

Suppose advance theme is overriding the function and the same function if plugin overrides, than plugin is overriding function of advance theme and this breaking things completely.

Now this I have tested with only one theme and one plugin but I afraid that is possible to override function over and over again by one and another plugin with is realy need to take concern.

So is there any way to keep it always and yet can add overridden items to the same functions from all other plugins?
Q2A version: 1.5.4
by
Correct!.. so do you have any solution for this?

1 Answer

+2 votes
by
selected by
 
Best answer

Yes, for function overrides you can call through to the "base" function as it says in the documentation:

When overriding functions, you should call through to the _base function as much as possible.

For theme layers it's similar, you should call through to the parent function. It can be a bit complex, especially if you want to change something that the parent function outputs. Generally you should just change variables. I can't think of a great example right now, but let's pretend the default CSS function is like this:

function css()
{
  $this->output('<link rel="stylesheet" href="'.$this->cssfile.'">');
}

If you want to output an extra CSS file you would do:

function css()
{
  parent::css();
  $this->output('<link rel="stylesheet" href="newfile/css">');
}

Or if you want to change what CSS file is output you would do something like this in your plugin:

function css()
{
  $this->cssfile = 'newfile.css';
  parent::css();
}

 

by
Just to add a related question regarding your user-activity-plugin and Noah's history plugin: http://www.question2answer.org/qa/20654/history-plugin-and-user-activity-plus-plugin-conflict
by
Thanks Scott for your detailed answer.
So you mean instead of using
qa_html_theme_base::css()

I should use

parent::css(); ?

Or something below

function css()
{
// here my own code
return css_base();
}

Is that correct? In fact I thought it allows only to call system function not theme function ( views ).
by
@Kai,

Yes this is exactly what is happening now. If we use qa_html_theme_base::function()
As it is always overriding theme function with plugin functions. Also need some information how plugin stacked on each other so it helps to understand which plugin override what.
by
@pixelngrain you should be able to use qa_html_theme_base::css() too, because that is the parent class. "parent" is just shorter and easier :)
by
Oh yeh you are right!. let me give it a try again with multiple override on the same function and get back to you.
by
@Scott alright! tested with same layer with two different plugin and it works fine. Thanks... :)
by
@pixelngrain: So this issue is gone? ... I mean do you have two plugins with layers that both override a core function? And both sustain their code(s) now?
by
Actually I partially it worked but today I was working on one plugin and this issue again pops up. I have started another question here http://www.question2answer.org/qa/21060/overiding-function-reopened-the-question
by
thx for letting me know
...