search
HomeBackend DevelopmentPHP TutorialPHP 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的类:

<&#63;php  
class A {  
  public function __construct() {  
    echo 'Got it.';  
  }  
}

然后我们有一个index.php需要用到这个类A,常规的写法就是

<&#63;php  
  require('A.php');  
  $a = new A();

但是有一个问题就是,假如我们的index.php需要包含的不只是类A,而是需要很多类,这样子就必须写很多行require语句,有时候也会让人觉得不爽。

__autoload()自动加载
不过在php5之后的版本,我们就不再需要这样做了。
在php5中,试图使用尚未定义的类时会自动调用__autoload函数,所以我们可以通过编写__autoload函数来让php自动加载类,而不必写一个长长的包含文件列表。

例如在上面那个例子中,index.php可以这样写:

<&#63;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 ]]] )
我们继续改写上面那个例子:

<&#63;php  
function loader($class){  
  $file = $class . '.php';  
  if (is_file($file)) {  
    require_once($file);  
  }  
}  
spl_autoload_register('loader');  
$a = new A();

或者直接使用匿名函数:

<&#63;php  
 
spl_autoload_register(function($file){
  $file = $class . '.php';  
  if (is_file($file)) {  
    require_once($file);  
  }
});  
$a = new A();

这样子也是可以正常运行的,这时候php在寻找类的时候就没有调用__autoload而是调用我们自己定义的函数loader了。同样的道理,下面这种写法也是可以的:

<&#63;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

<&#63;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)

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1117027.htmlTechArticlephp自动加载方式集合,php加载方式集合 php加载文件方式: 1、include,include_once,requice,requice_one常规加载 2、__autoload() 3、spl_autoload_register() 常...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools