• PHP学习笔记——Php文件引入


    在工作日常开发中,我最常用的文件引用莫过于include(include_once),以及require(require_once),这两种引用已经满足了日常简单CGI的开发需要,但是当深入了解PHP框架的内容,就会发现单单这两个关键字远远不够,本文整理了部分php文件引用所涉及的知识。

     

    首先简要的介绍一下include和require的用法

    1.include

    被包含文件先按参数给出的路径寻找,如果没有给出目录(只有文件名)时则按照 include_path(定义于php配置文件)指定的目录寻找。如果在 include_path 下没找到该文件则 include 最后才在调用脚本文件所在的目录和当前工作目录下寻找。

     

    2.require语句

    require include 几乎完全一样,除了处理失败的方式不同之外。require 在出错时产生 E_COMPILE_ERROR 级别的错误。换句话说将导致脚本中止,而 include 只产生警告(E_WARNING),脚本会继续运行。

     

    3.autoload机制

    __autoloader函数是默认用于载入未知PHP类的函数,当再php代码中出现了未知类时,程序将会自动调用该函数,函数原型及一种简单的实现如下

    function __autoload($classname) {
    
        $filename = "./". $classname .".php";
    
        include_once($filename);
    
    }

    通常在各个类型不同的类中可以实现自己的__autoload()函数,如果希望注册自己的__autoLoade函数则可以使用spl中的spl_autoload_register(),该函数可以将自定义的函数放入autoloader队列当中,当autoloader被触发是,将会依次调用队列中的函数。

    spl_autoload_register()原型如下

     

    bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]] )

     

    spl_autoload_register可以被调用多次,将多个自定义的回调函数插入到autoload函数队列当中。

     

     

    __autoloader()函数在ZEND引擎中有一个默认的实现,就是spl_autoload(),当不愿意自己实现__autoloader()函数的时候,可以使用这个php自带的载入函数,通常该函数会和spl_autoload_extensions()函数一起使用,spl_autoloader_extensions()可以指定所包含类所在文件的扩展名,spl_autoload()函数会依据类名+扩展名的形式,在include_path当中寻找对应类所在的文件。之后在代码中调用不含参数的spl_autoload_register()就可以在该程序中调用默认的spl_autoload()函数。

     

    A. class.php

    <?php
    
    class A{ 
    
    function write(){
    
    echo “I am A”;
    
    }
    
    }
    
    ?>

     

    B.php

    <?php
    
    spl_autoload_extensions(“.class.php”);
    
    spl_autoload_register();
    
    $a=new A();  
    
    $a->write();  // I am A
    
    ?>

     

    spl_autoload_call()函数的目的是为了让我们可以手动去调用spl_autoload_register()注册队列中的回调函数,常规情况下,只有遇到未知类才会调用autoloader队列中的回调函数,spl_autoload_call可以实现手动调用,依次调用队列中的回调函数直到成功加载到未知类。

    A. php

    <?php
    
     class A{}
    
    ?>

     

    B. php

    <?php
    
    function A($class){ echo "A";};
    
    function B($class) {echo "B";include($class.".php");};
    
     
    
    spl_autoload_register(A);
    
    spl_autoload_register(B);
    
    spl_autoload_call("A");  //输出 AB
    
    ?>

     

    4.use 关键字

    use语句可以引用其他名空间中的类。Php名空间的作用跟其他语言的名空间作用一样,用于解决第三方的类中变量及函数的冲突问题。

    A.php

    <?php
    
    namespace TEST;
    
    class A{
    
    function myfunc(){
    
    echo "this A::myfunc 
    ";
    
    }
    
    }

     

    B.php

    <?php
    
    use TESTA;
    
    require_once("A.php");
    
    class B{
    
    function myfunc(){
    
    print "this B::myfunc 
    ";
    
    }
    
    }
    
    $b=new B();
    
    $b->myfunc();
    
    $a=new A;
    
    $a->myfunc();
    
     

    输出:

    this B::myfunc 

    this A::myfunc

  • 相关阅读:
    npm ci fast install All In One
    WebGL & CG All In One
    StackOverflow winterbash 2021 All In One
    jira advanced searching All In One
    localStorage undefined bug All In One
    vcharts bug All In One
    crypto.getRandomValues & Math.random All In One
    Web 3D 技术 All In One
    企业微信如何设置用户当前状态 All In One
    parent.postMessage bug All In One
  • 原文地址:https://www.cnblogs.com/sworddance/p/4280736.html
Copyright © 2020-2023  润新知