一、__autoload

这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数。

在index.php中,由于没有包含test.class.php,在实例化printit时,自动调用__autoload函数,参数$class的值即为类名printit,此时test.class.php就被包含进来了。

eg:

test.class.php 

<?php 

class test { 

 function doPrint() {
echo 'hello world';
}
}
?> index.php <?
function __autoload( $class ) {
$file = $class . '.class.php';
if ( is_file($file) ) {
require_once($file);
}
} $obj = new test();
$obj->doPrint();
?>

二、spl_autoload_register()

spl_autoload_register(),这个函数与__autoload有与曲同工之妙,将__autoload换成自定义函数。但是该自定义函数不会像__autoload自动触发,这时spl_autoload_register()就起作用了,它告诉PHP碰到没有定义的类就执行该自定义函数()。

<? 

class test {
public static function myAuto( $class ) {
$file = $class . '.class.php';
if (is_file($file)) {
require_once($file);
}
}
} class myTest {
  function doPrint() {
  echo 'hello world';
}
} spl_autoload_register( array('test',myAuto) ); //另一种写法:spl_autoload_register( "test::myAuto" ); $obj = new myTest ();
$obj->doPrint();
?>
 

spl_autoload_register() && __autoload函数的更多相关文章

  1. 【PHP面向对象(OOP)编程入门教程】23.自动加载类 __autoload()函数

    很多开发者写面向对象的应用程序时,对每个类的定义建立一个 PHP 源文件.一个很大的烦恼是不得不在每个脚本(每个类一个文件)开头写一个长长的包含文件的列表. 在软件开发的系统中,不可能把所有的类都写在 ...

  2. PHP __autoload函数知识点

    __autoload函数主要是用来包含不存在的类文件,当初始化的类不存在的时候 存在一个文件名为footer.php的文件,里面有个footer类 class footer{ public funct ...

  3. __autoload函数

    ./index.php----------------------------------------------------------------------------------<?ph ...

  4. php __autoload函数 加载类文件

    面向对象的开发时,大家肯定都会遇到这样的问题,就是加载文件,一般都是加文件的头部inclue_once,require一大堆,看着很让人烦.当然你可以自己写程序来加载.php5以后引入了__autol ...

  5. php对象:__autoload()函数及单入口文件,__set(), __get(), get_class_methods(),get_class_vars()

    __autoload():当类中找不到相关类的时候,会自动执行__autoload()函数,可以自动加载相关文件 __set() : 当对类的私有变量进行调用赋值时,自动调用该方法.  __get() ...

  6. PHP __autoload函数(自动载入类文件)的使用方法(转)

    详细出处参考:http://www.jb51.net/article/29625.htm 在使用PHP的OO模式开发系统时,通常大家习惯上将每个类的实现都存放在一个单独的文件里,这样会很容易实现对类进 ...

  7. php中 __autoload函数 自动加载类文件机制

    魔术函数,自动加载机制function __autoload($class_name) {   //自动传递的是类名$path = str_replace('_', '/', $class_name) ...

  8. PHP函数spl_autoload_register()用法和__autoload()介绍(转)

    详细出处参考:http://www.jb51.net/article/29624.htm 又是框架冲突导致__autoload()失效,用spl_autoload_register()重构一下,问题解 ...

  9. php 加载函数 __autoload(), spl_autoload_register()

    来自:http://www.cnblogs.com/myluke/archive/2011/06/25/2090119.html spl_autoload_register (PHP 5 >= ...

随机推荐

  1. WPF中退出时显示是否保存数据提示

    一.通过窗体中的按钮实现退出时数据保存提示 Xaml: <Grid> <TextBlock HorizontalAlignment="Left" Margin=& ...

  2. Java poi 的使用

    poi可操作老旧版本的excel 下载jar包,http://archive.apache.org/dist/poi/release/bin/poi-bin-3.17-20170915.tar.gz ...

  3. 理解 React,但不理解 Redux,该如何通俗易懂的理解 Redux?(转)

    作者:Wang Namelos 链接:https://www.zhihu.com/question/41312576/answer/90782136来源:知乎 解答这个问题并不困难:唯一的要求是你熟悉 ...

  4. 题解 P5082 【成绩】

    随机跳题跳到了这一题,一看是个红题,本蒟蒻就 艰难地思考起来 高兴地写起来 这题实在不能用数组,用了数组就RE 一开始就卡在这上面了 说实话,这道题真的 很难 不算很难,只要照着公式往上面套就行了 废 ...

  5. windows搭建gcc开发环境(msys2) objdump

    前言 可能你并不太了解msys2,但是作为一个程序员,你一定知道mingw,而msys2就集成了mingw,同时msys2还有一些其他的特性,例如包管理器等. msys2可以在windows下搭建一个 ...

  6. java socket domain name 使用域名.

    java 的 socket 依赖了 nameService.  引擎模式. 使得 socket tcp 层 具有了上层业务的能力 (应用层) Socket socket=new Socket(&quo ...

  7. https 调用验证失败 peer not authenticated

    https 调用验证失败 peer not authenticated 报错日志: Caused by: javax.net.ssl.SSLPeerUnverifiedException: peer ...

  8. shell脚本,如何写进度条。

    [root@localhost ~]# cat jindutiao.sh #!/bin/bash #进度条 n=$((/)) N=$((/)) ` do sleep 0.01 [ $(($i%$n)) ...

  9. 628. Maximum Product of Three Numbers@python

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  10. linux设备驱动程序 - 待解决问题记录

    1.每个模式都有自己的内存映射,也即自己的地址空间?(P26) http://www.cnblogs.com/wuchanming/p/4360277.html (不知道是不是,没时间看)