Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    returning function from other function in same subclass

    I am creating a plugin for Joomla and ran into problems with the right way to use PHP. After two days of trying and reading and trying I am completely stuck, and I hope someone can help me!

    What I am trying to do is to call a function (which returns a variable) from another function in the same sub-class in order to put that between divs and create the final output in the function that is called by the system.
    My problem is that nothing I have tried gets one function to return the other function.

    To be more precise, dependent on a condition I want to either replace something in the article text or add to the article text. I did actually manage to get the other function returned in order to replace something in the article text, but I just cannot find a way to do so in order to add to the article text.
    Since I managed to replace by using preg_replace_callback I thought I might manage to add by using call_user_func, but no luck so far. Obviously I am doing something wrong with the PHP code but having tried many things I have no idea what would be right.

    What I have is the following:

    PHP Code:
    defined'_JEXEC' ) or die( 'Restricted access' );

    jimport('joomla.plugin.plugin');

    class 
    plgContentExample extends JPlugin
    {

        function 
    plgContentExample( &$subject$params 
       {

          
    parent::__construct$subject$params );

          
    $plugin =& JPluginHelper::getPlugin('content''example');
          
    $pluginParams = new JParameter$plugin->params );

       }

        function 
    my_example($matches
       {

          
    $outputTEST "Testing my example plugin";
          return 
    $outputTEST;

       }

        function 
    onPrepareContent( &$article, &$params$limitstart 
       {

          global 
    $mainframe;

          
    $plugin =& JPluginHelper::getPlugin('content''example');
          
    $pluginParams = new JParameter$plugin->params );

          if(
    $mycondition==1)
         {
             
    $regex "#{(callmyexample)\s*(.*?)}#";
             
    preg_match_all$regex$article->text$matches );
             
    $count count$matches[0] );
             if ( 
    $count 
            {
                
    $article->text preg_replace_callback($regex
                array(&
    $this'my_example'), $article->text);//THIS WORKS
            
    }
         } else {

          
    $myfunctionoutput call_user_func('my_example');//THE TROUBLESOME LINE
          //$myfunctionoutput = 'hi there';//WITH THIS THE REST WORKS
          
    $mydisplayoutput "<br/><div>".$myfunctionoutput."</div>";
          
    $article->text $article->text $mydisplayoutput;

         }

       }


    Could anyone help me find what I should put instead of the troublesome bit?

    Many many thanks in advance!

    E

  • #2
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    i haven't used Joomla but i think u need to do this...

    PHP Code:
    $myfunctionoutput call_user_func('my_example');//THE TROUBLESOME LINE 
    //i aint to sure whether or not you can do it that way ^^ from within a class

    //need to be like
    $myfunctionoutput $this->my_example();//will now run the function 
    hope this helps


  • #3
    New Coder
    Join Date
    Oct 2007
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Alas... I just tried it but it didn't work. Thanks for trying to help though!

  • #4
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    what error message did u get or just a empty page?

    I have used many different OOP PHP systems and i have always used "$this->function_name()" to call a function in the same class...

    the function within your class has a variable set

    PHP Code:
        function my_example($matches)  //unused var $matches
       


          
    $outputTEST "Testing my example plugin"
          return 
    $outputTEST

       } 
    this could throw an error/warning because the var isn't set correctly, you could try setting a default value for this or just remove it.
    Last edited by mic2100; 12-11-2008 at 02:49 PM.

  • #5
    New Coder
    Join Date
    Oct 2007
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Another thing I never figured out was how to see errors

  • #6
    New Coder
    Join Date
    Oct 2007
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry I should be more specific..
    If I remove $matches the result is the same: the page shows as normal but where there should be 'Testing my example plugin' there simply is nothing (except "SD Wrap" which I inserted as shown below). However, without $matches the preg_replace_callback does not work anymore.

    The actual code I am working on is as below:
    PHP Code:
    defined'_JEXEC' ) or die( 'Restricted access' );

    jimport('joomla.plugin.plugin');
     
    class 
    plgContentgetDrs extends JPlugin
    {

        function 
    plgContentgetDrs( &$subject$params 
        {

            
    parent::__construct$subject$params );

            
    $plugin =& JPluginHelper::getPlugin('content''getdrs');
            
    $pluginParams = new JParameter$plugin->params );

        }
     
        function 
    get_drs($matches
        {

            
    /*deleted lots of code to create the value of $sd_output*/

            
    $sd_outputTEST "Testing getDrs";
            return 
    $sd_outputTEST;

        }

        function 
    onPrepareContent( &$article, &$params$limitstart 
        {
     
            global 
    $mainframe;
     
            
    $plugin =& JPluginHelper::getPlugin('content''getdrs');
            
    $pluginParams = new JParameter$plugin->params );

            
    $doeventlist $pluginParams->get('doeventlist',0); 

            
    $option JRequest::getVar('option'null);
            
    $view JRequest::getVar('view'null);
            
            if ( 
    $view == 'article' ) {

                
    $regex "#{(getdrs)\s*(.*?)}#";
                if ( !
    $pluginParams->get'enabled') ) {
                    
    $article->text preg_replace$regex''$article->text );
                    return 
    true;
                }
                
    preg_match_all$regex$article->text$matches );
                
    $count count$matches[0] );
                if ( 
    $count ) {
                    
    $article->text preg_replace_callback($regex, array(&$this'get_drs'), $article->text);
                }

            } else if ( 
    $option='com_eventlist' && ( $view='details' || $view='venueevents' ) ) {

                if ( 
    $pluginParams->get'enabled') == 1  && $doeventlist == ) {
                    
    $myfunctionoutput $this->get_drs();
                    
    $thisismyoutput "<br/><div>SD Wrap ".$myfunctionoutput."</div>";
                    
    $article->text $article->text $thisismyoutput;
                }

            }

        }



  • #7
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    do it simply then, remove all the code that u have removed for the example (or comment out) also change $matches to $matches = '', and get it working without passing the variable to start with once u have it working then start to add the rest of the code.

    like this
    PHP Code:
    defined'_JEXEC' ) or die( 'Restricted access' ); 

    jimport('joomla.plugin.plugin'); 
      
    class 
    plgContentgetDrs extends JPlugin 


        function 
    plgContentgetDrs( &$subject$params )  
        { 

            
    parent::__construct$subject$params ); 

            
    $plugin =& JPluginHelper::getPlugin('content''getdrs'); 
            
    $pluginParams = new JParameter$plugin->params ); 

        } 
      
        function 
    get_drs($matches '')  
        { 

            
    $sd_outputTEST "Testing getDrs"
            return 
    $sd_outputTEST

        } 

        function 
    onPrepareContent( &$article, &$params$limitstart )  
        { 
      
            global 
    $mainframe
      
            
    $plugin =& JPluginHelper::getPlugin('content''getdrs'); 
            
    $pluginParams = new JParameter$plugin->params ); 

            
    $doeventlist $pluginParams->get('doeventlist',0);  

            
    $option JRequest::getVar('option'null); 
            
    $view JRequest::getVar('view'null); 
             
            if ( 
    $view == 'article' ) { 

                
    $regex "#{(getdrs)\s*(.*?)}#"
                if ( !
    $pluginParams->get'enabled') ) { 
                    
    $article->text preg_replace$regex''$article->text ); 
                    return 
    true
                } 
                
    preg_match_all$regex$article->text$matches ); 
                
    $count count$matches[0] ); 
                if ( 
    $count ) { 
                    
    $article->text preg_replace_callback($regex, array(&$this'get_drs'), $article->text); 
                } 

            } else if ( 
    $option='com_eventlist' && ( $view='details' || $view='venueevents' ) ) { 

                if ( 
    $pluginParams->get'enabled') == 1  && $doeventlist == ) { 
                    
    $myfunctionoutput $this->get_drs(); 
                    
    $thisismyoutput "<br/><div>SD Wrap ".$myfunctionoutput."</div>"
                    
    $article->text $article->text $thisismyoutput
                } 

            } 

        } 


    now it shud just return the $sd_outputTEST wen the function is called.

  • #8
    New Coder
    Join Date
    Oct 2007
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Brilliant! That made it work

    For the replacing however I needed the $matches, but your suggestion to change $matches to $matches = '' gave me the idea to change that back but to change $this->get_drs() to $this->get_drs(''), and that also worked!

    Great, now both things work after all those hours of trying!

    Out of interest, could I also use $this-> to get a variable out of another function in the same (sub)class? What would be good keywords to Google for $this and learn more about it?

    Thanks for your help!!

  • #9
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    if you want to retrieve a value from the class you create a var at the start of the class this can then be called into any function within that class by using $this->var1 (or what ever you want to call ur variable).

    an example....
    PHP Code:
    class new_class
    {

      var 
    $var1 10;
      var 
    $var2 20;

      function 
    one()
      {

        
    $value $this->var1 $this->var2;

        echo 
    $value//will echo the number 30

      
    }


    this tutorial might help

    http://www.phpfreaks.com/tutorial/oo...elations/page1
    Last edited by mic2100; 12-12-2008 at 07:11 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •