


PHP automatic loading method collection, PHP loading method collection_PHP tutorial
php自动加载方式集合,php加载方式集合
php加载文件方式:
1、include,include_once,requice,requice_one常规加载
2、__autoload()
3、spl_autoload_register()
常规加载方式
假设我们有一个类文件A.php,里面定义了一个名字为A的类:
<?php class A { public function __construct() { echo 'Got it.'; } }
然后我们有一个index.php需要用到这个类A,常规的写法就是
<?php require('A.php'); $a = new A();
但是有一个问题就是,假如我们的index.php需要包含的不只是类A,而是需要很多类,这样子就必须写很多行require语句,有时候也会让人觉得不爽。
__autoload()自动加载
不过在php5之后的版本,我们就不再需要这样做了。
在php5中,试图使用尚未定义的类时会自动调用__autoload函数,所以我们可以通过编写__autoload函数来让php自动加载类,而不必写一个长长的包含文件列表。
例如在上面那个例子中,index.php可以这样写:
<?php function __autoload($class){ $file = $class . '.php'; if (is_file($file)) { require_once($file); } } $a = new A();
当然上面只是最简单的示范,__autoload只是去include_path寻找类文件并加载,我们可以根据自己的需要定义__autoload加载类的规则。注意:由于__autoload()是个函数,只能存在一次。
spl_autoload_register()自动加载
但现在问题来了,如果在一个系统的实现中,如果需要使用很多其它的类库,这些类库可能是由不同的开发人员编写的,其类名与实际的磁盘文件的映射规则不尽相同。这时如果要实现类库文件的自动加载,就必须在__autoload()函数中将所有的映射规则全部实现,这样的话__autoload()函数有可能 会非常复杂,甚至无法实现。最后可能会导致__autoload()函数十分臃肿,这时即便能够实现,也会给将来的维护和系统效率带来很大的负面影响。在这种情况下,难道就没有更简单清晰的解决办法了吧?答案当然是:NO!
spl_autoload_register() 满足了此类需求。 它实际上创建了 autoload 函数的队列,按定义时的顺序逐个执行。相比之下, __autoload() 只可以定义一次。
bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]] )
我们继续改写上面那个例子:
<?php function loader($class){ $file = $class . '.php'; if (is_file($file)) { require_once($file); } } spl_autoload_register('loader'); $a = new A();
或者直接使用匿名函数:
<?php spl_autoload_register(function($file){ $file = $class . '.php'; if (is_file($file)) { require_once($file); } }); $a = new A();
这样子也是可以正常运行的,这时候php在寻找类的时候就没有调用__autoload而是调用我们自己定义的函数loader了。同样的道理,下面这种写法也是可以的:
<?php class Loader { public static function loadClass($class){ $file = $class . '.php'; if (is_file($file)) { require_once($file); } } } spl_autoload_register(array('Loader', 'loadClass')); //spl_autoload_register(array(__CLASS__, 'loadClass')); //spl_autoload_register(array($this, 'loadClass')); $a = new A();
更多示例
autoload.php
<?php define('SDK_PATH', __DIR__); require_once SDK_PATH . '/common/functions.php'; require_once SDK_PATH . '/common/config.php'; spl_autoload_register(function ($class) { if (false !== stripos($class, 'YJC\Wechat')) { require_once __DIR__ . '/' . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 10)) . '.php'; } }); /* function __autoload($class){ if (false !== stripos($class, 'YJC\Wechat')) { require_once __DIR__ . '/' . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 10)) . '.php'; } } */
建议入口文件里养成定义绝对地址SDK_PATH的习惯,这样require不会出错。
以上就是本文的全部内容,希望对大家的学习有所帮助。
您可能感兴趣的文章:
- php基础知识:类与对象(2) 自动加载对象
- php面向对象全攻略 (十七) 自动加载类
- php自动加载的两种实现方法
- PHP的autoload自动加载机制使用说明
- PHP spl_autoload_register实现自动加载研究
- 说说PHP的autoLoad自动加载机制
- PHP autoload与spl_autoload自动加载机制的深入理解
- PHP管理依赖(dependency)关系工具 Composer的自动加载(autoload)

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software