CMSApplicationWhen Joomla does its initial routing in Joomla\CMS\Application\CMSApplication in the route method, the JUri object is cloned. The line is:

$uri = clone \JUri::getInstance();

Looking at the code for \JUri::getInstance(), when called without a parameter, it will contain the URI based on the SERVER variables.

Why is this cloned, rather than just assigned?

1 Answer 1


Object cloning is done so that subsequent actions on the object are applied purely to the "detached replica" (cloned object) and not the original object.

By not cloning, in other words just declaring the preexisting object as a new variable, you enjoy (or not) the effects of using a reference variable. In all usages and for the life of the two variables, all actions on either variable -- will be "shared" and will affect both objects.

There is more to this story regarding "shallows copies" and "deep copies", but I'm probably not well positioned to speak on this. If there are fringe cases that defy the truths that I have stated above, then I am unaware of them.

I've extended this Stack Overflow snippet to show the marriage between non-cloned objects, and the independence of cloned objects. Demo

I have an example where cloning is beneficial during the process of building a mysql query with separate Joomla database objects. Scroll to the bottom of my answer at https://joomla.stackexchange.com/a/24148/12352

  • Thanks for the answer. I understand the differences between cloning an assignment of objects, my question was specifically about theJUri object and why its cloned rather than just assigned
    – Dom
    Jul 8, 2019 at 10:50
  • I am not at my computer right now, nor have I looked up the line of code in the core, but I'll venture that a second and untethered copy of the object was required for a subsequence process. I'll have a closer look when I can -- but that might be this weekend. Jul 8, 2019 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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