PHP automatically loads objects (taking the MVC framework as an example)-php Tutorial

Source: Internet
Author: User
Tags autoloader
PHP automatically loads objects (using the MVC framework as an example) & lt ;? Phpclassautoloader {publicstatic $ loader; publicstaticfunctioninit () {if (self: $ loaderNULL) self ::$ loadernewself (); returneuron PHP automatically loads objects (take the MVC framework as an example)
 

1. when the program uses an undeclared class, the _ autolaod () function is automatically called for loading;

  

2. spl_autoload_register () is used to register an automatically called function. you can register multiple functions!

3. $ iniPath = ini_get ('include _ path'); ini_set ('include _ path', $ iniPath .. $ cPath); set environment variables to achieve autoload. set the include path. you can directly include the files in these directories without writing detailed paths. Method 3: use php. MVC.

 Setup the application class paths (PHP 'include_path') for the included* class files, for the duration of the main script

**

Returns the class path string for testing purposes** @depreciated* @param stringThe appServerRootDir. eg: 'C:/Www/phpmvc'* @param arrayAn array of sub-application paths,
* eg: $subAppPaths[] = 'WEB-INF/classes/example';, ...* @param stringThe OS [Optional] [UNIX|WINDOWS|MAC|...] if we have* trouble detecting the server OS type. Eg: path errors.* @public* @returns string*/function setClassPath($appServerRootDir='', $subAppPaths='', $osType='') {// Set AppServer root manually for nowif($appServerRootDir == '') {echo 'Error: ClassPath :- No php.MVC application root directory specified';exit;}#$_ENV;// PHP Superglobals !!// Setup the main phpmvc application include() directories here// Note: could be placed in a n xml config file later !!$appDirs = array();$appDirs[] = ''; // application root directory$appDirs[] = 'lib';// Add the sub-application paths, if anyif(is_array($subAppPaths)) {$appDirs = array_merge($appDirs, $subAppPaths);}// 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 4global $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 scriptini_set('include_path', $classPath);return $classPath;// for testing}// ----- Public Methods ------------------------------------------------- //function getClassPath($appServerRootDir='', $appDirs, $osType='') {// Set AppServer root manually for nowif($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.* * @paramstringThe path string* @paramstringThe path string* @paramstringThe operating type [optional]* @public* @returnsstring*/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.** @paramstringThe operating type [optional]* @protected* @returnsstring*/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 4global $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 stringThe 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;}}?>

?

?

 

? Call method autoloader. php

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.