PHP风格指南[关闭]

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:

  • only one single class per file
  • decide for a spelling case in names for class and method names and stay with that. I myself use smallCamelCase.
  • opening curly brackets on the line of the opening language construct, except when opening a method on first level or a class
  • indentation of 2 blanks per level, most modern editors allow to enter tabs and have them converted into 2 spaces
  • in-code documentation preceding every class, method and function, so that you can use phpdoc or the like
  • use namespaces, but not too exessively
  • use what visibility selectors php offers (private, public, ...), although php is pretty dull in that, often you are forced to make things public, whyever. Nevertheless note them down.
  • use single quotes for 'technical identifiers' and double quotes for "human readable text", typically for output.
  • use class constants wherever possible
  • static class methods often allow cleaner code and save you from instanciating redundant objects
  • use upper case spelling for constants and language contructs like TRUE and FALSE
  • prevent string concatenations using the period operator, use sprintf() instead
  • put a clear comment header at the top of each file stating the name of the file, its purpose, the author(s), the license. Use the same structure of header throughout all files, even across language borders (sql files or js or the like)

From 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/