How do I go about calling a function (method) in one plugin from another plugin.

peregrineperegrine MVP
edited March 2013 in Feedback
        default.php( of PluginOne)

      snippet....
        class PluginOne extends Gdn_Plugin {

        public function test1($Sender) {
         // do something
        }

what would be the procedure (syntax) to call test1  from within   PluginTwo



      default.php (of PluginTwo)
     class PluginTwo extends Gdn_Plugin {   

   public function Testing ($Sender) {
                // call to test1 in pluginOne

               ??? what would be the correct syntax
                   ??????????::test1($Sender);

            }

Comments

  • AFAIK, it is not possible unless:

    • $Sender has an object reference to PluginOne that is visible to PluginTwo
    • You are in a scope that contains an object reference of both
    • PluginTwo extends PluginOne

    I think the first option would be best (although I doubt there is a reference in $Sender). Otherwise I would extend the first plugin. It would be something along the lines of:

      class PluginTwo extends PluginOne {
        public function test1 ($Sender) {
        // call to test1 in pluginOne
        parent::test1($Sender);
      }
    
  • thanks @hgtonight

    it gives me some ideas. I'll see what $Sender knows. I don't think option 2 is going to work the way I want to do things.

  • edited March 2013

    You can call other plugins by using Gdn::PluginManager().

    Simplest example:

    // Retrieve plugin by class name
    $PluginInstance = Gdn::PluginManager()->GetPluginInstance('PluginOne');
    
    // Same as above, passing the Sender
    $PluginInstance = Gdn::PluginManager()->GetPluginInstance('PluginOne', Gdn_PluginManager::ACCESS_CLASSNAME, $Sender);
    
    // Retrieve plugin by plugin name (i.e. the one declared in Plugin information array
    $PluginInstance = Gdn::PluginManager()->GetPluginInstance('NameOfPluginOne', Gdn_PluginManager::ACCESS_PLUGINNAME);
    
    // Same as above, passing the Sender
    $PluginInstance = Gdn::PluginManager()->GetPluginInstance('NameOfPluginOne', Gdn_PluginManager::ACCESS_PLUGINNAME, $Sender);
    
  • peregrineperegrine MVP
    edited March 2013

    .

  • @businessdad

    perfecto, I learned something new today.

  • @peregrine said:
    businessdad
    perfecto, I learned something new today.

    You're welcome. :)

Sign In or Register to comment.