Re: Question about Code Style

From: Date: Wed, 18 Aug 2010 14:27:16 +0000
Subject: Re: Question about Code Style
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-53681@lists.php.net to get a copy of this message
On Mon, 2010-08-16 at 21:58 +0200, Jesús Espino wrote: > Hi: > > I have a problem to remove some (1137) Warnings of PHPCS on PHP_Shell package. > > I have a file with prototypes of functions, builded like the example: > > > protected $prototype = array ( > 'XMLReader::close' => array ( > 'return' => 'boolean', > 'params' => '', > 'description' => 'Closes xmlreader - current frees > resources until xmlTextReaderClose is fixed in libxml', > ), > 'XMLReader::getAttribute' => array ( > 'return' => 'string', > 'params' => 'string name', > 'description' => 'Get value of an attribute from current > element', > ), > ... > ); > > The problem is the description field, i can't wrap to 80 characters > without changing the meaning. > > I can't concatenate in various strings in various lines because is a > dictionary and not accept the "." operator. > > Any eficient porpose?? Must be eficient, because the file is included > in the main Class and is really big (588K). > Hi Jesús, You can, in fact, concatenate strings in an array definition in PHP. The following code is valid, for example: $prototype = array( 'test' => 'foo' . 'bar', ); The specific problem you're running into here is you can _only_ assign literals as default values for instance variables. There are a couple of things you could do here: 1.) Do nothing. PHPCS won't pass, but not being able to wrap lines in default values seems like a PHP limitation, not poor coding style. 2.) Initialize the property as an empty array in the class definition and properly initialize it during class construction. If the property is also static, create a static initializer that runs when the first call to construct is made. Example: <?php class Foo { protected $bar = array(); protected static $staticBar = array(); public function __construct() { // initialize 'bar' using string concatenation $this->bar = array('foo' => 'bar' . 'baz'); // static initialization of 'staticBar' happens only once static $initialized = false; if (!$initialized) { self::$staticBar = array('foo' => 'bar' . 'baz'); $initialized = true; } } } ?> Hope this helps. Cheers, Mike

« previous php.pear.dev (#53681) next »