note 70898 added to language.oop5.autoload
| From: | andrew dot delete dot cornes at gmail dot delete dot com | Date: | Fri, 03 Nov 2006 12:26:50 +0000 |
| Subject: | note 70898 added to language.oop5.autoload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119334@lists.php.net to get a copy of this message | ||
If you'd like '__autoload()' to support multiple class folders, each containing
multiple class files (one per class), you may want to try something like this (file
'__autoload.php'):
<?php
define('CLASS_FILENAME_SUFFIX', '.class.php');
function __autoload($className)
{
$__autoloadAbsolutePath = dirname(__FILE__);
// 'pathStart' is your web application root folder.
// (This may or may not be where '__autoload.php'
// resides; let's assume here that it resides one
// level 'below' the web app root.)
$pathStart = $__autoloadAbsolutePath .
DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR;
// 'classPath' is a list of class folders to look in.
// (In this example, there's just one: 'classlibs/lib1'.
// To add more, simply append them; start with
// 'PATH_SEPARATOR . $pathStart .', and off you go...)
$classPath = PATH_SEPARATOR . $pathStart .
'classlibs' . DIRECTORY_SEPARATOR . 'lib1';
// Add list of class folders to 'include_path' for the
// forthcoming 'require()' (or similar directive).
$oldIncludePath = get_include_path();
set_include_path($oldIncludePath . $classPath);
require_once($className . CLASS_FILENAME_SUFFIX);
// Reinstate initial 'include_path'.
set_include_path($oldIncludePath);
}
?>
As your web application develops, new paths containing class files can be added into the
'$classPath' variable within '__autoload()'. If hard-coding the
'$classPath' variable isn't to your taste, you could arrange for its value to come
from 'outside' in whatever way you like.
Any comments gratefully received.
----
Server IP: 85.116.0.130
Probable Submitter: 86.20.160.201
----
Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php
Edit -- https://master.php.net/note/edit/70898
Del: integrated -- https://master.php.net/note/delete/70898/integrated
Del: useless -- https://master.php.net/note/delete/70898/useless
Del: bad code -- https://master.php.net/note/delete/70898/bad+code
Del: spam -- https://master.php.net/note/delete/70898/spam
Del: non-english -- https://master.php.net/note/delete/70898/non-english
Del: in docs -- https://master.php.net/note/delete/70898/in+docs
Del: other reasons-- https://master.php.net/note/delete/70898
Reject -- https://master.php.net/note/reject/70898
Search -- https://master.php.net/manage/user-notes.php