Spl_autoload_register () can really do different third-party class libraries using their own self-loading?

Source: Internet
Author: User
Tags autoload spl
namespace A1;
function A ($name)
{
Echo $name;
}
Spl_autoload_register (' a1\a ');
namespace A2;
function A ($name)
{
Echo $name;
}
Spl_autoload_register (' a2\a ');
New A; Instantiate the a2\a, but why does it trigger the a1\a before triggering the a2\a
The way I use the file reference is still the same, but if a1\a found the class file, it will not be triggered a2\a, the registered load function "does not belong to the namespace", then how can the different third-party class libraries use their own registered self-loading it?

Reply content:

namespace A1;
function A ($name)
{
Echo $name;
}
Spl_autoload_register (' a1\a ');
namespace A2;
function A ($name)
{
Echo $name;
}
Spl_autoload_register (' a2\a ');
New A; Instantiate the a2\a, but why does it trigger the a1\a before triggering the a2\a
The way I use the file reference is still the same, but if a1\a found the class file, it will not be triggered a2\a, the registered load function "does not belong to the namespace", then how can the different third-party class libraries use their own registered self-loading it?

Each library ignores classes that it cannot load.

It's true............

Well, I've only read the title of the main topic. At the moment to tidy up the main code, try to elaborate ...

php
  
   

问题 1:为啥先调 \spaceA\loadA ?

随便哪个顺序都没啥影响吧?再说,按你传入的顺序调有啥不妥咩?

问题 2: 怎么能做到不同第三方类库使用自己的注册的自加载呢?

不同类库的类名不一样就行了,担心类名冲突的话,不是有命名空间咩?它实际就是类名的前缀,使用时把类名写全就行了。比如:

phpnew \spaceA\Something()new \spaceB\Something()

问题 3: 效率低怎么办?

效率低也要看低在哪里

    • 查找类文件的路径太慢?
      可以提前建立索引,通常会提前建好类名(注意命名空间其实就是类名的前缀)到类定义文件的映射,方便你通过完整的类名直接取到类定义的文件路径。或者像Java那样规范好类的目录,正如 fig 这个社区组织所整理的的 PSR-4 那样。

    • 类加载函数调用的次数太多?
      每个类定义文件只会加载一次,runtime找到类的定义后就不再调用你的"类加载函数"了。一般项目里能引用多少三方库?能注册多少个加载函数?能有多少类文件?万一得用十万百万千万来计数的话,回头请给个基准测试数据过来摔在在下的脸上,顺便也摔在php开发组的脸上,到时大家一起边掐边哭边研究下下hiahiahia…………。

    • 预留问题(在下暂时想不出了……)

关于spl-autoload-register的使用,详情请参考文档 http://php.net/manual/zh/function.spl-autoload-register.php

我懂了,http://www.thinkphp.cn/document/474.html
spl_autoload_register() 会将一个函数注册到 autoload 函数列表中,当出现未定义的类的时候,SPL [注] 会按照注册的倒序逐个调用被注册的 autoload 函数,这意味着你可以使用 spl_autoload_register() 注册多个 autoload 函数.
逐个的调用被注册的自动函数,这多么的不理智啊,被注册的自动函数应该“属于某个命名空间”才好啊,项目的各个类库还是存在很很小的耦合性的,万一耦合,加载不该加载的那不就完了啊,真是的,我的理解对不对呢?

  • 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.