Skip to main content Link Search Menu Expand Document (external link)

PHP Coding Standards

  1. Overview
  2. Joomlashack Additions
  3. Comments
  4. Autoloading
    1. PSR-4
    2. Joomla subclasses

Overview

We have adopted PSR-12 as much as is possible. In the context of Joomla conventions, it is almost required to break some of these standards. Exceptions are permitted when there is no alternative. For example, when overriding a Joomla parent class or method. Typical exceptions include, but are not limited to:

  • PSR-1 Side Effects must not mix with declaration
  • PSR-1 Class namespace required
  • PSR-1 Class names in PascalCase
  • PSR-1 Method/Property names in camelCase
  • PSR-2 Method/Property names must not start with ‘_’

Joomlashack Additions

  • Always use array short syntax. e.g. $a = ['key' => 'value'];

Comments

Comments are encouraged when they help to bring attention to a block of code or what the code is doing is not obvious. Overly obvious comments are discouraged. For example:

// Access check
if (!Factory::getUser()->authorise('core.manage')) {
    throw new Exception(Text::_('JERROR_ALERTNOAUTHOR'), 404);
}

We require the use of PHP DocBlocks for all class methods. @return should always be included even if the return type is void. For methods that inherit from a parent class, this should be indicated with:

    /**
     * @inheritDoc
     */
    public static function getInstance()
    {
        ...
    }

Autoloading

NOTE: This description refers to code that is currently in major flux. We are transitioning to a new Joomlashack deployment system from our original Alledia deployment system. The general principles have not changed only the reference to repositories currently in development.

Joomlashack uses an in-house AutoLoader class to handle both autoloading scenarios described below.

PSR-4

In our own frameworks and independent code that is not restricted by Joomla conventions and standards, we adhere to the PSR-4 Autoloader standards. This is implemented with with the AutoLoader::register() method.

Joomla subclasses

In some of our extensions, we implement a subclassing system that provides easy overriding of core Joomla classes with a camelCase oriented autoloading convention. This is implemented with the AutoLoader::registerCamelBase() method.


Copyright © 2020-2022 Joomlashack. License GPL