Re: Coding Standards

From: Date: Tue, 22 Jun 2004 11:56:38 +0000
Subject: Re: Coding Standards
References: 1 2  Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-4541@lists.php.net to get a copy of this message
> > $foo = (bool)$foo; > > $foo = ( bool ) $foo; > > $foo = (bool) $foo; > > So, which format are you choosing? It seems to me the vast majority of > code already in the repository, and elsewhere, uses the first format, so > it seems best to make that the standard. Although I prefer the first, the third is more accepted and "proper". I suggest we go with that. > > define ('CAT_PACK_BASE', basename(__FILE__)); > > require_once CAT_PACK_BASE . 'Driver/mysql.php'; > > Yuck. Defining constants is expensive. Plus it obfuscates what's being > included. True, but it's faster and allows you to check if a file exists before including it. Using @include 'foo'; sucks because parse errors in foo aren't shown which can be confusing. > > The page on using include/require needs to be explained better. Require > > should be used in all cases unless the script will run if the include fails > > (ie optional include). > > The page seems clear to me. Perhaps you can email the list with the full > text you are thinking of in order to get some feedback? Upon reading it again, it's actually not that bad. Here's some additions: Anywhere you are unconditionally including a class file, and this will be most cases with a few exceptions, use <function>require_once</function>. Anywhere you are conditionally including a class file (for example, factory methods), use <function>include_once</function>. The only places where <function>include_once</function>/<function>require_once</function> should not be used, is when a specific value (for example, the value of a variable which is global to the include) is required. In this case, <function>require</function> should be used. Either of these will ensure that class files are included only once. They share the same file list, so you don't need to worry about mixing them - a file included with <function>require_once</function> will not be included again by <function>include_once</function>. You should not use parentheses around the filename. <code type="php"> require_once 'FooBar.php'; include 'BarFoo.php'; </code> It's not great... I think it probably needs to be more specific, whatever :)

« previous php.pear.doc (#4541) next »