HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

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);

            }

I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

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);
      }
    

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • 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.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • 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

    .

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • @businessdad

    perfecto, I learned something new today.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

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

    You're welcome. :)

Sign In or Register to comment.