Function Calls

Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon. Here's an example:

<?php
$var 
foo($bar$baz$quux);
?>

As displayed above, there should be one space on either side of an equals sign used to assign the return value of a function to a variable. In the case of a block of related assignments, more space may be inserted to promote readability:

<?php
$short         
foo($bar);
$long_variable foo($baz);
?>

To support readability, parameters in subsequent calls to the same function/method may be aligned by parameter name:

<?php

$this
->callSomeFunction('param1',     'second',        true);
$this->callSomeFunction('parameter2''third',         false);
$this->callSomeFunction('3',          'verrrrrrylong'true);
?>

Split function call on several lines

The CS require lines to have a maximum length of 80 chars. Calling functions or methods with many parameters while adhering to CS is impossible in that cases. It is allowed to split parameters in function calls onto several lines.

<?php

$this
->someObject->subObject->callThisFunctionWithALongName(
    
$parameterOne$parameterTwo,
    
$aVeryLongParameterThree
);
?>

Several parameters per line are allowed. Parameters need to be indented 4 spaces compared to the level of the function call. The opening parenthesis is to be put at the end of the function call line, the closing parenthesis gets its own line at the end of the parameters. This shows a visual end to the parameter indentations and follows the opening/closing brace rules for functions and conditionals.

The same applies not only for parameter variables, but also for nested function calls and for arrays.

<?php

$this
->someObject->subObject->callThisFunctionWithALongName(
    
$this->someOtherFunc(
        
$this->someEvenOtherFunc(
            
'Help me!',
            array(
                
'foo'  => 'bar',
                
'spam' => 'eggs',
            ),
            
23
        
),
        
$this->someEvenOtherFunc()
    ),
    
$this->wowowowowow(12)
);
?>

Nesting those function parameters is allowed if it helps to make the code more readable, not only when it is necessary when the characters per line limit is reached.

Using fluent application programming interfaces often leads to many concatenated function calls. Those calls may be split onto several lines. When doing this, all subsequent lines are indented by 4 spaces and begin with the "->" arrow.

<?php

$someObject
->someFunction("some""parameter")
    ->
someOtherFunc(2342)
    ->
andAThirdFunction();
?>

Alignment of assignments

To support readability, the equal signs may be aligned in block-related assignments:

<?php

$short  
foo($bar);
$longer foo($baz);
?>

The rule can be broken when the length of the variable name is at least 8 characters longer/shorter than the previous one:

<?php

$short 
foo($bar);
$thisVariableNameIsVeeeeeeeeeeryLong foo($baz);
?>

Split long assigments onto several lines

Assigments may be split onto several lines when the character/line limit would be exceeded. The equal sign has to be positioned onto the following line, and indented by 4 characters.

<?php

$GLOBALS
['TSFE']->additionalHeaderData[$this->strApplicationName]
    = 
$this->xajax->getJavascript(t3lib_extMgm::siteRelPath('nr_xajax'));
?>
Control Structures (Previous) Class Definitions (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: masiena@hotmail.com
"In a well written code you shouldn't use find/replace. Especially a mindless find/replace. If so take your time. "

Sure?

If you ALREADY have bad code, you will do find/replace. If you refactor code, you will do find/replace. If you don't like your predecessor's coding style in the project at hand, you will do find/replace.

In all that cases (and other, I suppose), extra spaces just get in the way. By the way, find/replace that uses REs get complicated by extra spaces.

Just some thoughts of someone that has been in the battle field. For the last 25 years.

Cheers.


Note by: user@example.com
In a well written code you shouldn't use find/replace. Especially a mindless find/replace. If so take your time.
Note by: aheinz@lulu.com
Adding spaces for horizonal justification improves readability until you do a search/replace on a variable name. This practice should be discouraged.