PSR-2 demands that a file either defines classes and functions or contains side-effects. Including

defined('_JEXEC') or die;

in all files collides with this convention. Is it safe to exclude this line?

1 Answer 1


In theory yes. This line is designed to stop remote file inclusion. So if you're in any file that only has a class then you are effectively safe. The only component files you'd need this in are your html default.php (and any other template files). So in the html outputting files the component entry file it's absolutely compulsory and in all other files it doesn't do any harm but isn't strictly necessary

In practice we can't scan for this kind of requirement easily in our automated JED checker so we require this in every file.

So ultimately it depends if you want to be listed on JED or not. If it's a custom component then just the html output and component entry files should have it (and it's unlikely either will be adhering to PSR-2 anyhow). If it's something you are distributing on JED then yes in every file is required. I'd still err on the side of including it in every file personally whatever the use case.

I also think you're reading a bit too much into PSR-2 here - I could argue preventing direct access to a file isn't what it means as a side-effect. They mean about having the class as the only responsibility. This check is only about the file itself - yes it's not to do with the class but it's not about doing other side tasks. It's just asking if this file has been directly accessed or whether it's being included by the Joomla CMS Application

  • Sure, the main reason to fix all PSR-2 warnings - even if they are trivial as in this case - is to be more awake when something is wrong. Commented Jan 31, 2017 at 16:32
  • Olle Härstedt, any way you can't go with full PSR-2 and Joomla without some "hacks", reason - I need you classes to be at least vendor namespaced. @GeorgeWilson there is no reading to much into PSR-2, it's default PHPCS PSR-2 check warning message, so I believe this is not situation where meaning can be discussed as those are checker warnings.
    – Alexandr
    Commented Jan 31, 2017 at 21:28
  • Any plans on making Joomla components PSR-2 compatible? Commented Feb 1, 2017 at 11:13
  • Almost certainly not. We are going to add namespaces in Joomla 4, but I don't think we are going to break all outstanding PR's to go from tabs to spaces and also the _JEXEC checks will almost certainly never get removed. Other than those two items we are aiming for compliance I think Commented Feb 2, 2017 at 12:42
  • We do have our own coding standards that long predate PSR-2 github.com/joomla/coding-standards - so of course these will trend towards PSR-2 but given that we have support in PHPStorm for our style guide etc. There's no real rush for the project Commented Feb 2, 2017 at 12:43

Your Answer

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

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