There are several ways to store variables either just for the running execution of the script, or persistently.
In the case where you want to make a variable in a component available to a module, you will use the set and get methods of the Application object. You might use the following example to write a shop component and require a module to show related products based on the selected product:
In the component:
$mainframe->set( 'shop.currentProduct', $product );
Then in the module, to retrieve that varible:
$product = $mainframe->get( 'shop.currentProduct' );
if (is_object( $product ))
{
// do something
}
Another example you can use is for a tracking plugin that is triggered late in the execution of the Joomla! script.
In the component:
$mainframe->set( 'tracking.title', $row->title );
In the plugin:
$title = $mainframe->get( 'tracking.title' );
$url = $_SERVER['QUERY_STRING'];
$ip = $_SERVER['REMOTE_ADDR'];
$query = 'INSERT INTO #__tracker' .
' SET ip = ' . $db->Quote( $ip ) .
' url = ' . $db->Quote( url_encode( $url ) .
' title = ' . $db->Quote( $title );
$db = &JFactory::getDBO();
$db->setQuery( $query );
if (!$db->query())
{
JError::raiseWarning( '500', 'A problem occurred' );
return false;
}
To persist the variable in the session, that is, have it available between different pages, you use the setUserState and getUserState methods in the Application object, for example:
$cart = &$mainframe->getUserState( 'shop.cart' ); $cart->addProduct( $product ); $mainframe->setUserState( 'shop.cart', $cart );
A final example is where you want to persist a filter on a list screen, for example when you select a category on a list of articles. In this case you would use the getUserStateFromRequest method in the Application object.
$catid = $mainframe->getUserStateFromRequest( 'com_content.list.catid', 'catid', 0 );
Where:
component_option.view.variable_name is useful)
This method is a short hand for using a combination of getUserState and setUserState. Many Administrator components use this technique so there are plenty of real examples to follow.