...

View Full Version : returning function from other function in same subclass



ewel
12-11-2008, 01:38 PM
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:


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

mic2100
12-11-2008, 02:00 PM
i haven't used Joomla but i think u need to do this...



$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

:)

ewel
12-11-2008, 02:17 PM
Alas... I just tried it but it didn't work. Thanks for trying to help though!

mic2100
12-11-2008, 02:44 PM
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



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.

ewel
12-11-2008, 02:55 PM
Another thing I never figured out was how to see errors :(

ewel
12-11-2008, 03:22 PM
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:

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', 1 ) ) {
$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 ) == 1 && $doeventlist == 1 ) {
$myfunctionoutput = $this->get_drs();
$thisismyoutput = "<br/><div>SD Wrap ".$myfunctionoutput."</div>";
$article->text = $article->text . $thisismyoutput;
}

}

}

}

mic2100
12-11-2008, 04:47 PM
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


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', 1 ) ) {
$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 ) == 1 && $doeventlist == 1 ) {
$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.

ewel
12-11-2008, 09:42 PM
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!!

mic2100
12-12-2008, 07:08 PM
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....


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-php-part-3-uml-classes-and-relations/page1



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum