[Coding Standards] (require|include)_once not allowed
| From: | Paul M Jones | Date: | Mon, 09 Jul 2007 20:52:09 +0000 |
| Subject: | [Coding Standards] (require|include)_once not allowed | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-47329@lists.php.net to get a copy of this message | ||
The proposed standard states:
*_once including of files not allowed
include/require/require_once/include_once is not allowed for loading class files except in the "load all classes for beginners" file. Instead, class_exists() with the optional second parameter should be used. Instead of:
<?php
require_once 'PEAR2/OtherPackage.php';
?>
this should be used:
<?php
if (!class_exists('PEAR2_OtherPackage', true)) {
throw new PEAR2_PackageName_Exception('Dependency PEAR2_OtherPackage not found');
}
?>
This allows packages to work unmodified both inside a phar archive and extracted to disk, and provides end users a much more flexible choice of how to load classes.
While "phar" is wonderful, I am wary of writing a general coding standard with one particular tool in mind. This would be like saying "don't use function X because a particular IDE doesn't work well with it".
Can anyone here tell me why or how the class_exists() + throw() solution is a "more flexible choice" for class loading, so much so that require_once() should be prohibited?
This seems like another candidate for "recipe" or "practice" to me, not a rule by which package acceptance can be denied.
--
Paul M. Jones <http://paul-m-jones.com>
Solar: Simple Object Library and Application Repository
for PHP5. <http://solarphp.com>
Join the Solar community wiki! <http://solarphp.org>
Savant: The simple, elegant, and powerful solution for
templates in PHP. <http://phpsavant.com>