The following code works in Joomla 1.6, but it throws up a Strict Standards error in 3.x:

Strict Standards: Only variables should be assigned by reference

    //Get the full URL for sharing purposes
    $uri = & JFactory::getURI();
    $absolute_url = $uri->toString();

    <?php echo $absolute_url; ?>

How can I get the code to work without the error in Joomla 3.x?



The answer to the main question is: JFactory::getUri() is deprecated, use JUri directly like this:

$uri = JUri::getInstance(); 
echo $uri->toString();

// or simpler:

echo JUri::getInstance();

The Strict Standards error is because of the & before JFactory::getURI();. You can find more information about that below.


Delete the &

//Get the full URL for sharing purposes
$uri = JFactory::getURI();
$absolute_url = $uri->toString();

<?php echo $absolute_url; ?>

With PHP5 the new operator automatically returns a reference, so the '&' is not needed anymore.

From the official PHP manual:

As of PHP 5, the new operator returns a reference automatically, so assigning the result of new by reference results in an E_DEPRECATED message in PHP 5.3 and later, and an E_STRICT message in earlier versions.

source: http://php.net/manual/en/language.operators.assignment.php


According to the Joomla tutorial, you can get the current URL by this function:


JFactory::getURI(); is deprecated. The correct method to get the current URL is:

$uri = JUri::getInstance();
echo  $uri->toString();

Relying on the toString method to get the current URL is wrong. The toString method is not meant for that. The following is a better, cleaner, and future proof approach to retrieve the current URL:

$objJURI = JFactory::getURI();
$strQuery = '';
if (!empty($objJURI->getQuery()))
    $strQuery = '?'.$objJURI->getQuery();
$strFragment = '';
if ((!empty($objJURI->getFragment())) && ($objJURI->getFragment() != '#'))
    $strFragment = $objJURI->getFragment();
$currentURL = $objJURI->current().$strQuery.$strFragment;

Note that the getFragment method should return the part after the number sign, but there seems to be a core bug that prevents it from doing that.

  • This is wrong. It will display index.php&option=com_foo&view=bar. It should be ? after index.php – Lodder Oct 10 '16 at 11:55
  • 2
    Its absolutely legit to use toString. Its a public method and heavily used by the JUri class itself. Even the method comment says "Returns full uri string.". Using JFactory::getUri(); is deprecated and definitely "more wrong". – fruppel Oct 10 '16 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.