MODX PHP Coding Standards
Last updated Feb 27th, 2021 | Page history | Improve this page | Report an issue
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$306 per month—let's make that $500!
Learn moreThese coding standards primarily apply to PHP code.
General¶
- Beginning brackets do NOT linebreak. They start one space after the end parenthesis, as according to traditional Unix policy.
- Do not do any real logic in object constructors. Create class methods to do so.
null
,true
andfalse
should always be lowercase. - Avoid embedded assignments (ex:
$d = ($a = $b + $c)
is bad). - Never use
extract()
. - Avoid using global variables if at all possible.
- Document EVERYTHING.
Parenthesis¶
- Do not put parenthesis next to keywords. Put a space between.
- Do put parenthesis next to function names.
- Do not use parenthesis in return statements when it's not necessary. Example:
if ($test) {
}
while ($test == $other) {
}
array_push($one,$two);
return $test;
- Do not use parenthesis when using
include
,require
,include_once
, andrequire_once
.
Classes¶
- All ''core'' classnames, unless stated otherwise for special conditions, will be prefixed with the "mod" prefix: ie, modChunk, modTemplate, etc.
- All method names will be camelCase and will start with a lowercase letter.
- All private methods and variables must be prefixed with the underscore _ character.
class modFactor {
public $publicVar;
private $_privateVar;
private function _privateFunc() { }
public function publicFunc() { }
}
Variables¶
Note these are not function arguments.
- Use all lowercase letters.
- Separate words with the underscore.
Function Arguments and Class Variables¶
- The first letter is lowercase, rest are camelCase. Example:
class modFactor {
public function testFunc($testVar, array &$anotherTest = array()) {
$this->_privateVar = $testVar;
$local_variable =& $anotherTest;
}
}
Arrays¶
- Array index names use the underscore _, not the dash as their separator. This prevents errors with
magic_quotes
. - Array index names are always lowercase. Spaces are represented by an underscore.
- Array index names are always encapsulated with single quotes. Example:
$_lang['chunk_create_text'] = 'Test';
Constants¶
- Constants must be in all UPPERCASE letters.
- Use only if absolutely necessary.
File Structure¶
- Always name PHP class files in name.class.php format.
Prefixing¶
- Lexicon strings for Components need to be prefixed:
$_lang['mycomponent.welcome_message'] = 'Welcome!';
- Always prefix class names; eg: 'finBank', 'finTransaction', etc.
- Always prefix Chunk names; eg: 'finStatement', 'finDeposit'
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$306 per month—let's make that $500!
Learn more