PHP 自动加载类
来源:互联网 发布:qq管家域名检测 编辑:程序博客网 时间:2024/05/17 23:47
__autoload
./myClass.php<?phpclass myClass { public function __construct() { echo "myClass init'ed successfuly!!!"; }}?>./index.php<?php// we've writen this code where we needfunction __autoload($classname) { $filename = "./". $classname .".php"; include_once($filename);}// we've called a class ***$obj = new myClass();?>
spl_autoload_register
<?php if(!function_exists('classAutoLoader')){ function classAutoLoader($class){ $class=strtolower($class); $classFile=$_SERVER['DOCUMENT_ROOT'].'/include/class/'.$class.'.class.php'; if(is_file($classFile)&&!class_exists($class)) include $classFile; } } spl_autoload_register('classAutoLoader');?>
加载类的2中方式:
方式一:
For example, let's say I have an app that has a base path defined as PATH_APP
, and a simple structure with directories namedmodels
,views
and controllers
. I often employ a naming structure whereby files are namedIndexView.php
andIndexController.php
inside the appropriate directory, and models generally have no particular scheme by default. I might have a loader function for this structure like this that gets registered withspl_autoload_register
:
public function MVCLoader($class){ if (file_exists(PATH_APP.'/models/'.$class.'.php')) { require_once(PATH_APP.'/models/'.$class.'.php'); return true; } else if (strpos($class,'View') !== false) { if (file_exists(PATH_APP.'/views/'.$class.'.php')) { require_once(PATH_APP.'/views/'.$class.'.php'); return true; } } else if (strpos($class,'Controller') !== false) { if (file_exists(PATH_APP.'/controllers/'.$class.'.php')) { require_once(PATH_APP.'/controllers/'.$class.'.php'); return true; } } return false;}
方式二:
public static function loadClass($class){ $files = array( $class . '.php', str_replace('_', '/', $class) . '.php', ); foreach (explode(PATH_SEPARATOR, ini_get('include_path')) as $base_path) { foreach ($files as $file) { $path = "$base_path/$file"; if (file_exists($path) && is_readable($path)) { include_once $path; return; } } }}
If I look for SomeClass_SeperatedWith_Underscores it will look for SomeClass_SeperatedWith_Underscores.php followed by SomeClass/SeperatedWith/Underscores.php rooted at each directory in the current include path.
0 0
- php 自动加载类
- PHP自动加载类
- PHP 自动加载类
- PHP 自动加载类
- PHP 类自动加载
- PHP自动加载类
- php自动加载类
- PHP:自动加载类
- PHP类的自动加载
- php类自动加载机制
- php的自动加载类
- 65. PHP 自动加载类
- php类的自动加载
- php自动加载类 Test
- PHP类的自动加载
- PHP - 类自动加载机制
- php实现自动加载类
- PHP类的自动加载
- objdump命令的使用
- iOS开发UI篇—iPad和iPhone开发的比较
- 斯特灵(Stirling)数
- linux常用命令(4):mkdir命令
- UVA 455 Periodic Strings
- PHP 自动加载类
- Binary Tree Paths 二叉树遍历
- 鼠标样式外边距的合并
- Java(02)-JDK 5.0 新特性
- Map有序存储数据
- mysql ERROR 1045 (28000): Access denied for user解决方法
- 伪元素
- word批量修改图片大小
- 获取ios项目名称及版本号