您好,欢迎访问一九零五行业门户网

PHP 自动加载对象(以MVC框架替例)

php 自动加载对象(以mvc框架为例)

1, 在程序使用未声明的类时会自动调用 __autolaod() 函数来加载;

2.其中 spl_autoload_register() 用来注册一个自动调用的函数, 可以注册多个函数!
3.$inipath = ini_get('include_path');ini_set('include_path', $inipath. . $cpath);通过设置环境变量来达到autoload目的,设置包含路径,以后可以直接包含这些目录中的文件,不需要再写详细的路径了。方法三取自php.mvc,使用参照php.mvc文档
4 ) { // php 5 $windir = $_env[windir]; // see: php v.4.1.0 superglobals } else { // php 4 global $http_env_vars; // depreciated- if( array_key_exists(windir, $http_env_vars) ) { $windir = $http_env_vars[windir]; // will be replaced with $_env } } if($ostype != '') { if( eregi(windows, $ostype) ) { $delim = ';'; // windows } elseif( eregi(unix, $ostype) ) { $delim = ':'; // unix } elseif( eregi(mac, $ostype) ) { $delim = ':'; // mac !!!!! } } if($delim == null) { if( eregi(win, $windir) ) { // _env[c:\\win2k] $delim = ';'; // windows } else { $delim = ':'; // unix, mac !! } } // get the current working directory $path = $appserverrootdir; // strip path directories below 'web-inf' $pathtowebinf = ereg_replace(web-inf.*$, '', $path); // replace path backslashes with forward slashes // note: php regular expressions do not work with backslashes $pathtowebinf = str_replace(\\, /, $pathtowebinf); // drop the trailing slash, if one is present $pathtowebinf = ereg_replace(/$, '', $pathtowebinf); // setup the environment path string $classpath = null; foreach($appdirs as $appdir) { $classpath .= $pathtowebinf.'/'.$appdir.$delim; } // remove trailing delimiter character $classpath = substr($classpath, 0, -1); // setup the include_path for the duration of the main php.mvc script ini_set('include_path', $classpath); return $classpath; // for testing } // ----- public methods ------------------------------------------------- // function getclasspath($appserverrootdir='', $appdirs, $ostype='') { // set appserver root manually for now if($appserverrootdir == '') { echo 'error: classpath :- no php.mvc application root directory specified'; exit; } #$_env; // php superglobals !! // setup the platform specific path delimiter character $delim = null; // path delimiter character. (windows, unix, mac!!) if($ostype == '') { // php's build in constant path_separator [unix (:) / win (;)] $delim = path_separator; } else { // it is handy to be able to specift the os type for testing $delim = classpath::getpathdelimiter($ostype); } // get the current working directory $path = $appserverrootdir; // strip path directories below 'web-inf' $pathtowebinf = ereg_replace(web-inf.*$, '', $path); // replace path backslashes with forward slashes // note: php regular expressions do not work with backslashes $pathtowebinf = str_replace(\\, /, $pathtowebinf); // drop the trailing slash, if one is present $pathtowebinf = ereg_replace(/$, '', $pathtowebinf); // setup the environment path string $classpath = null; $absolutepath = false; // say: /some/unix/path/ or d:\some\win\path foreach($appdirs as $appdir) { // check if the specified system path is an absolute path. absolute system // paths start with a / on unix, and ch\: or ch/: on win 32. // eg: /some/unix/path/ or d:\some\win\path or d:/some/win/path. $absolutepath = classpath::absolutepath($appdir); if($absolutepath == true) { $classpath .= $appdir.$delim; } else { $classpath .= $pathtowebinf.'/'.$appdir.$delim; } } // remove trailing delimiter character $classpath = substr($classpath, 0, -1); return $classpath; // for testing } /** * concatenate environment path strings * * returns the two path strings joined with the correct environment * string delimiter for the host operating system. * * @param string the path string * @param string the path string * @param string the operating type [optional] * @public * @returns string */ function concatpaths($path1, $path2, $ostype='') { // setup the platform specific path delimiter character $delim = null; // path delimiter character. (windows, unix, mac!!) $delim = classpath::getpathdelimiter($ostype); $path = $path1 . $delim . $path2; return $path; } // ----- protected methods ---------------------------------------------- // /** * get environment path delimiter. * * returns the environment string delimiter for the host operating system. * * @param string the operating type [optional] * @protected * @returns string */ function getpathdelimiter($ostype='') { // setup the platform specific path delimiter character $delim = null; // path delimiter character. (windows, unix, mac!!) $windir = null; if( (int)phpversion() > 4 ) { // php 5 $windir = $_env[windir]; // see: php v.4.1.0 superglobals } else { // php 4 global $http_env_vars; // depreciated- if( array_key_exists(windir, $http_env_vars) ) { $windir = $http_env_vars[windir]; // will be replaced with $_env } } if($ostype != '') { if( eregi(windows, $ostype) ) { $delim = ';'; // windows } elseif( eregi(unix, $ostype) ) { $delim = ':'; // unix } elseif( eregi(mac, $ostype) ) { $delim = ':'; // mac !!!!! } } if($delim == null) { if( eregi(win, $windir) ) { // _env[c:\\win2k] $delim = ';'; // windows } else { $delim = ':'; // unix, mac !! } } return $delim; } /** * check if the specified system path is an absolute path. absolute system * paths start with a / on unix, and ch\: or ch/: on win 32. * eg: /some/unix/path/ or d:\some\win\path or d:/some/win/path. * * returns true if the suppplied path absolute, otherwise returns false * * @param string the path to check, like: /some/unix/path/ or * d:\some\win\path. * @public * @returns boolean */ function absolutepath($systempath) { // say: /some/unix/path/ or d:\some\win\path or d:/some/win/path $fabsolutepath = false; // boolean flag value //[/]some/unix/path/ if (preg_match(/^\//, $systempath)) { $fabsolutepath = true; //[d:\]some\win\path // i says ignore case // note the extra escape \ reqd for this to work with php !!! } elseif(preg_match(/^[a-z]:\\\/i, $systempath)) { $fabsolutepath = true; //[d:/]some/win/path } elseif(preg_match(/^[a-z]:\//i, $systempath)) { $fabsolutepath = true; } return $fabsolutepath; }}?>
??

?调用方法autoloader.php

其它类似信息

推荐信息