php自动加载autoload机制示例分享_PHP
1,自定义函数
2,spl_autoload_register()
复制代码 代码如下:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ ll ./*
-rw-rw-r-- 1 liuyuan liuyuan 800 Feb 19 11:39 ./func_autoload.php
-rw-rw-r-- 1 liuyuan liuyuan 906 Feb 19 11:28 ./spl_autoload.php
./include:
total 16
drwxrwxr-x 2 liuyuan liuyuan 4096 Feb 19 11:42 ./
drwxrwxr-x 3 liuyuan liuyuan 4096 Feb 19 11:43 ../
-rw-rw-r-- 1 liuyuan liuyuan 142 Feb 19 11:42 aClass.php
-rw-rw-r-- 1 liuyuan liuyuan 143 Feb 19 11:42 bClass.php
首先看自定义函数方式:
复制代码 代码如下:
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : ' br>');
print_r(get_included_files());
echo EOL;
print get_include_path();
echo EOL;
//set_include_path(get_include_path().PATH_SEPARATOR.'/var/www/ly_php/php_spl/include/');
//set_include_path(dirname(__FILE__).'/include');
//set_include_path(dirname(__FILE__).'/include/');
function __autoload($className){
$filename = './include/'.$className.'.php';
//$filename = './include/'.$className.'.php';
//$filename = '/var/www/ly_php/php_spl/include/'.$className.'.php';
if(file_exists($filename)){
include_once $filename;
}else{
exit('no file');
}
}
$a = new aClass();
$b = new bClass();
print_r(get_included_files());
?>
运行结果如下:
复制代码 代码如下:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ php func_autoload.php
Array
(
[0] => /var/www/phpgcs/php_autoload/func_autoload.php
)
.:/usr/share/php:/usr/share/pear
aClass is loaded
bClass is loaded
Array
(
[0] => /var/www/phpgcs/php_autoload/func_autoload.php
[1] => /var/www/phpgcs/php_autoload/include/aClass.php
[2] => /var/www/phpgcs/php_autoload/include/bClass.php
)
第二种方式:
复制代码 代码如下:
class myLoader{
public static function autoload($className){
$filename = './include/'.$className.'.php';
if(file_exists($filename)){
include_once $filename;
}else{
exit('no file');
}
}
}
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '
');
spl_autoload_register(array('myLoader', 'autoload'));
/**
*__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法
* 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list
*/
//spl_autoload_register( '__autoload' );
error_reporting(E_ALL^E_NOTICE^E_WARNING^E_ERROR);
error_reporting(E_NOTICE | E_WARNING );
$a = new aClass();
print_r(get_included_files());
echo EOL;
$b = new bClass();
echo EOL;
?>
运行结果如下:
复制代码 代码如下:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ php spl_autoload.php
aClass is loaded
Array
(
[0] => /var/www/phpgcs/php_autoload/spl_autoload.php
[1] => /var/www/phpgcs/php_autoload/include/aClass.php
)
bClass is loaded

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

As the PHP language becomes more and more popular, developers need to use more and more classes and functions. When a project grows in size, manually introducing all dependencies becomes impractical. At this time, an automatic loading mechanism is needed to simplify the code development and maintenance process. The auto-loading mechanism is a feature of the PHP language that can automatically load required classes and interfaces at runtime and reduce manual class file introduction. In this way, programmers can focus on developing code and reduce errors and time waste caused by tedious manual class introduction. In PHP, generally

Composer is a very popular dependency management tool in PHP. It can help us manage the third-party libraries and components needed in the project and automatically load these libraries and components. This article will introduce how to use Composer for automatic loading in PHP. Install Composer First, you need to install Composer. You can download the latest version of Composer at https://getcomposer.org/download/ and install it. InitializeComp

How can JavaScript achieve automatic scaling of content when scrolling to the bottom of the page and maintain the aspect ratio effect? In modern web design, scrolling to the bottom of the page to automatically load more content has become a common feature requirement. When the loaded content contains images, we often want these images to maintain their original aspect ratio. This article will introduce how to use JavaScript to implement this function and provide corresponding code examples for reference. First, we need to get the scroll position of the page. inJavaScr

Introduction to PHP Autoloading PHP autoloading is a mechanism that allows PHP to automatically load classes when needed without manually including the files. This greatly simplifies the development of large applications and improves code maintainability. Namespaces and Autoloading Namespaces in PHP are used to organize code. When a class declared using a namespace needs to be loaded, PHP will perform an automatic loading process. The autoloader is responsible for finding and loading the corresponding class files based on the namespace and class name. Automatic loading using Composer Composer is the standard tool in the PHP community for dependency management and automatic loading. After installing Composer, you can configure autoloading using the following steps: //composer.JSO

How does JavaScript achieve the fade-in effect of automatically loading content after scrolling to the bottom of the page? In modern web design, it is a very common requirement to scroll to the bottom of the page to automatically load content with a fade-in effect. This article will use JavaScript as an example to introduce how to achieve this effect. First, we need to use JavaScript to listen for page scroll events. When scrolling to the bottom of the page, we will trigger a function that loads new content. //Listen to the page scroll event window.addEv

How to use PHP7's namespace and automatic loading mechanism to organize the structure of the code? Abstract: With the launch of PHP7, namespace and automatic loading mechanism have become important features that cannot be ignored in PHP development. This article will introduce how to use PHP7's namespace and automatic loading mechanism to organize the structure of the code, and illustrate it through specific code examples. 1. What is a namespace? Namespace is a mechanism introduced in PHP7 to resolve naming conflicts that may occur between different class libraries or code files. via namespace

How does JavaScript achieve the gradient display effect of automatically loading content when scrolling to the bottom of the page? In modern web design, scrolling to the bottom of the page to automatically load content is a common requirement. In order to improve the user experience, gradient display effects are also a common design option. So, how do we implement it in JavaScript? Specific implementation steps and code examples are given below. The main idea to achieve this effect is to monitor the scroll event of the page and determine whether the bottom of the page has been reached based on the scroll position.

How to use PHP7's namespace and automatic loading mechanism to improve code readability and maintainability? In modern PHP development, code readability and maintainability are crucial factors. In order to better organize and manage code, PHP7 introduces namespace and automatic loading mechanism. By rationally using namespaces and automatic loading mechanisms, we can improve the readability and maintainability of our code. This article will introduce how to use PHP7's namespace and autoloading mechanism to achieve this goal, and provide specific code examples. 1. Fate
