xref: /PHP-7.3/ext/spl/examples/autoload.inc (revision 1c850bfc)
1<?php
2
3/** @file autoload.inc
4 * @ingroup Examples
5 * @brief function __autoload
6 * @author  Marcus Boerger
7 * @date    2003 - 2005
8 *
9 * SPL - Standard PHP Library
10 */
11
12/** \internal
13 * Tries to load class $classname from directory $dir.
14 */
15function __load_class($classname, $dir)
16{
17	$file = $dir . '/' . $classname . '.inc';
18	if (file_exists($file))
19	{
20		require_once($file);
21		return true;
22	}
23	return false;
24}
25
26/**
27 * @brief   Class loader for SPL example classes
28 * @author  Marcus Boerger
29 * @version 1.0
30 *
31 * Loads classes automatically from include_path as given by ini or from
32 * current directory of script or include file.
33 */
34function __autoload($classname) {
35	$classname = strtolower($classname);
36	$inc = split(':', ini_get('include_path'));
37	$inc[] = '.';
38	$inc[] = dirname($_SERVER['PATH_TRANSLATED']);
39	foreach($inc as $dir)
40	{
41		if (__load_class($classname, $dir))
42		{
43			fprintf(STDERR, 'Loading class('.$classname.")\n");
44			return;
45		}
46	}
47	fprintf(STDERR, 'Class not found ('.$classname.")\n");
48}
49
50?>
51