zoukankan      html  css  js  c++  java
  • php自动加载演变

    1.一开始我们调用不同页面的类里面的方法时

    //test.php
    require_once 'test1.php';
    require_once 'test2.php';
    Test1ceshi(); //调用方式  命名空间函数名
    Test2ceshi(); //调用方式  命名空间函数名
    
    namespace Test1;
    
    function ceshi(){
        echo __FILE__; //文件的完整路径和文件名  例如  D:sfcceshimooc	est1.php
    }
    namespace Test2;
    function ceshi(){
    	echo __FILE__; //文件的完整路径和文件名  例如 D:sfcceshimooc	est2.php
    }
    

    2.试想每一个文件都手动引入也太麻烦了,要是能用那个就引入就好了,有的有的,有个函数 function  __autoload(){ } 就可以实现

    //解决方案就是自定义自动加载函数,使用spl_autoload_register注册自动加载函数
    spl_autoload_register('autoload2');
    
    Test	est1::ceshi();//调用静态方法格式-->命名空间名类名::静态方法名
    TestTest2::ceshi();
    function autoload2($class){//定义引入文件函数 
        //echo $class ===> Test	est1
        list($namespace,$fileName) = explode('\',$class);
        require __DIR__.'\'.$fileName.'.php';
    }
    <?php
    namespace Test;
    
    class test1{
        public static function ceshi(){
            echo __METHOD__;//返回类的名字和方法的名字
    echo '</br>'; } }

      

    1 namespace Test;
    2 class test2{
    3     public static function ceshi(){
    4         echo __METHOD__; //返回类的名字和方法的名字
    5         echo '</br>';
    6     }
    7 }
  • 相关阅读:
    使用servicename连接Oracle数据库
    使用SID连接Oracle数据库
    使用xlrd模块
    【Project Euler 8】Largest product in a series
    Git使用帮助
    【Project Euler 1】Multiples of 3 and 5
    VSCode使用新体验
    导出牛顿引力形式为平方反比的两种方式
    NOIP2018游记
    即将退役声明
  • 原文地址:https://www.cnblogs.com/zxqblogrecord/p/8619784.html
Copyright © 2011-2022 走看看