Get instance of Plugin from other custom Plugin


I have two custom plugin’s who use generic functionality which resides in a third custom plugin. How can I get the instance of this third plugin from my first two custom plugin’s? I know that right before the event “onPluginsInitialized” all Plugins are initialized by the init() function in Plugins.php.

So how do i get an instance of the Plugin initialized by Grav? I don’t want to create a new instance when Grav already has one in memory.


I’m not sure what your trying to do with these three plugins and how they need to interact, but if you have simple classes and have loaded these classes when the plugin initializes, you should be able to just call the methods from the other plugins (as their classes should already be loaded).

The other option is to use Grav’s locator, and actually assign the objects to the locator, then look them up later from the other plugins.

Something like:

$this->grav['myplugin'] = new MyPlugin();

Then later you can simple get the instance and use it from another plugin:

$myplugin = $this->grav['myplugin'];

Perfect, works like a charm!