What is the best styling way of writing such a code in PHP?
$result = $this->funcA(param1, param2, param3, ...)->funcB(...)->funcC(...)
Also, what if the arguments are super long? How and where should you break them into multiple lines?
This is what I like:
$result = $this->funcAaAaA ( param1, param2, param3, ... )
->funcBb ( 'a', 'b' )
->funcCcc ( "word", TRUE )
Lot's of redundant whitespaces, yes. But it gives a very clear look and is easy to follow on a glance because logical blocks become very clear.
There are a few other things:
phpdoc
or the likeTRUE
and FALSE
period operator
, use sprintf()
insteadFrom here, 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.
Example:
<?php
$result=$this->funcA(param1, param2, ...)
->funcB(23, 42)
->funcC();
?>
To support readability, parameters in subsequent calls to the same function/method may be aligned by parameter name.
Example:
<?php
$this->callSomeFunction('param1', 'second', true);
$this->callSomeFunction('parameter2', 'third', false);
$this->callSomeFunction('3', 'verrrrrrylong', true);
?>
The same applies not only for parameter variables, but also for nested function calls and for arrays
Try to use these guidelines for you PHP code http://www.php-fig.org/psr/psr-2/