Re: Coding Standards

From: Date: Tue, 22 Jun 2004 11:00:13 +0000
Subject: Re: Coding Standards
References: 1 2  Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-4540@lists.php.net to get a copy of this message
> > 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. > But searching the include path is slow, and, it's not possible to see if the file exists before including it. This means you're using @include 'foo'; which means any errors thrown in that include are suppressed, which is a pain in the arse! > > 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? It's actually not so bad when I reread it, but here's some additions. Current: Anywhere you are unconditionally including a class file, use require_once(). Anywhere you are conditionally including a class file (for example, factory methods), use include_once(). 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 require_once() will not be included again by include_once(). Note: include_once() and require_once() are statements, not functions. You don't need parentheses around the filename to be included. Change to: Anywhere you are unconditionally including a class file, use <function>require_once</function>. Anywhere you are conditionally including a class file (for example, a factory pattern), use_include_once. For factory patterns, where if the code is unable to be included an error is thrownn, include_once If you're trying to retrieve a specific value from the file, for example the contents of a variable, use <function>require</function> Parentheses should not be used. <sometag type="php"> include 'foo.php'; require 'foo.php'; include_once 'foo.php'; require_once 'foo.php'; </sometag> > I'd like to see the CS standardize the format of calling includes to be: > require_once 'filename'; > rather than > require_once('filename'); Yep, me also.

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