PHP spl_autoload和class_exsits使用技能
来源:互联网 发布:艾磊软件 编辑:程序博客网 时间:2024/06/03 16:46
本文章的PHP使用版本:5.4.7
PHP建议使用:
spl_autoload_register
那么写了一种实现
文件路径
- core
- core.php
- ChildrenClass.php
- ParentClass.php
- test
- index.php
core.php:
<?php/** * Created by PhpStorm. * User: http://blog.csdn.net/cor_twi * Date: 2016/02/25 * Time: 11:23 */spl_autoload_register(function ($classId){ $dirname = dirname(__FILE__); $classIdParts = explode("\\", $classId); $classIdLength = count($classIdParts); $className = ($classIdParts[$classIdLength - 1]); $defaultNameSpace = substr(strrchr($dirname,"\\"),1); $namespace = $classIdLength==1? $defaultNameSpace : $classIdParts[0]; for ($i = 1; $i < $classIdLength-1; $i++) { $namespace .= '/' . $classIdParts[$i]; } $path = dirname(__FILE__) . "/../" . $namespace . "/" . $className . '.php'; $realPath = realpath($path); #print "autoload:realPath:" . $realPath."\n"; if (file_exists($realPath)) { if(!class_exists($className,false)){ $ret = include($realPath); print "spl_autoload_ autoload:$className is loaded : result = $ret \n"; } else print "spl_autoload_ autoload:$className is already loaded\n"; }});
ChildrenClass.php:
<?phpnamespace CORE;use CORE\ParentClass;/** * Created by PhpStorm. * User: http://blog.csdn.net/cor_twi * Date: 2016/02/22 * Time: 9:55 */class ChildrenClass extends ParentClass{ private $varPro_; public function __construct($var01,$var02) { parent::__construct($var01,$var02); $this->varPro_ = "ChildrenClass"; }}
ParentClass.php:
<?php/** * Created by PhpStorm. * User: http://blog.csdn.net/cor_twi * Date: 2016/02/22 * Time: 9:40 */namespace CORE;class ParentClass{ public static $variable01; public static $variable02; protected $varPro; /** *constructor */ public function __construct($var1,$var2){ self::$variable01 = $var1; self::$variable02 = $var2; $this->varPro = "ParentClass"; }}
index.php:
<?php/** * Created by PhpStorm. * User: http://blog.csdn.net/cor_twi * Date: 2016/03/05 * Time: 16:06 */require('../core/core.php');use CORE\ParentClass;use CORE\ChildrenClass;print "Section01------------------------\n\n";print "\nSection02------------------------\n\n";$instance01 = new ChildrenClass('aa','vv');$instance02 = new ParentClass('bb','uu');print "\nSection03------------------------\n\n";print "Class Load Test-----------------------\n";if(!class_exists('ChildrenClass')){ print "\n************\nWarning : in Section 03 : \nClass ChildrenClass is not loaded\n";}$interfaces = class_implements("ParentClass");print_r($interfaces);if(isset($interfaces['ParentClass'])){ print "OK,PASS";}else{ print "NO,IT'S NOT";}print "\nSection04------------------------\n\n";if(!class_exists('ChildrenClass')){ print "\n************\nTEST : in Section 04 : \nClass ChildrenClass is not loaded\n";}$rc = new ReflectionClass('ChildrenClass');try { if($rc->implementsInterface('ParentClass')){ print 'imples'; } print 'parent class:' . $rc->getParentClass();}catch(ReflectionException $e){}
然后输出结果是:
J:\xampp\php\php.exe E:\phpStormWorks\php01\test\index.phpSection01------------------------Section02------------------------spl_autoload_ autoload:ParentClass is loaded : result = 1 spl_autoload_ autoload:ChildrenClass is loaded : result = 1 Section03------------------------Class Load Test-----------------------************Warning : in Section 03 : Class ChildrenClass is not loadedspl_autoload_ autoload:ParentClass is loaded : result = 1 Warning: class_implements(): Class ParentClass does not exist and could not be loaded in E:\phpStormWorks\php01\test\index.php on line 29Call Stack: 0.0008 131888 1. {main}() E:\phpStormWorks\php01\test\index.php:0 0.0042 147400 2. class_implements() E:\phpStormWorks\php01\test\index.php:29NO,IT'S NOTSection04------------------------************TEST : in Section 04 : Class ChildrenClass is not loadedspl_autoload_ autoload:ChildrenClass is loaded : result = 1 Fatal error: Uncaught exception 'ReflectionException' with message 'Class ChildrenClass does not exist' in E:\phpStormWorks\php01\test\index.php on line 46ReflectionException: Class ChildrenClass does not exist in E:\phpStormWorks\php01\test\index.php on line 46Call Stack: 0.0008 131888 1. {main}() E:\phpStormWorks\php01\test\index.php:0 0.0079 147816 2. ReflectionClass->__construct() E:\phpStormWorks\php01\test\index.php:46Process finished with exit code 255
总结:
class_implements()
ReflectionClass()
class_exists()
$interfaces = class_implements(‘\CORE\TestImpl’);
对于当前版本来说:
这几个类的名字必须写use 的短语 as 的也不行:
例如:class_exists(‘\CORE\ParentClass’)
$interfaces[‘CORE\Interf’]
namespace前面的\无所谓
但是interfaces[‘CORE\Interf’] 前面的\有所谓!!!
PHP7:的报错是
PHP Fatal error: Cannot declare class CORE\ChildrenClass, because the name is already in use in E:\phpStormWorks\php01\core\ChildrenClass.php on line 29
PHP Stack trace:
PHP 1. {main}() E:\phpStormWorks\php01\test\index.php:0
PHP 2. class_implements() E:\phpStormWorks\php01\test\index.php:93
PHP 3. spl_autoload_call() E:\phpStormWorks\php01\test\index.php:93
<Reason>:由现象倒推而来,所有的PHP版本,对于有namespace的类或者接口,它是以 带namespace的形式autoload的,不管你写不写use
如果用include_once(),那么不在本文研究范围之内
写的时候手贱文章弄没有了一次,重写之。
测试结论仅供参考。
附上spl_autoload_实现:
<?php/** * Created by PhpStorm. * User: http://blog.csdn.net/cor_twi * Date: 2016/02/25 * Time: 11:23 */spl_autoload_register(function ($classId){ $dirname = dirname(__FILE__); $classIdParts = explode("\\", $classId); $classIdLength = count($classIdParts); $className = ($classIdParts[$classIdLength - 1]); $defaultNameSpace = substr(strrchr($dirname,"\\"),1); //here is core $namespace = $classIdLength==1? $defaultNameSpace : $classIdParts[0]; for ($i = 1; $i < $classIdLength-1; $i++) { $namespace .= '/' . $classIdParts[$i]; } $path = dirname(__FILE__) . "/../" . $namespace . "/" . $className . '.php'; $realPath = realpath($path); #print "autoload:realPath:" . $realPath."\n"; if(class_exists($classId,false)||interface_exists($classId,false)){//如果代码能进来这个函数说明没有被加载,便不会执行到这里,无法阻止Fatal error: Cannot redeclare class print "spl_autoload_ preCheck:$className | $classId | is already loaded\n"; } if (file_exists($realPath)) { if(class_exists($className,false)||interface_exists($className,false)){ print "spl_autoload_ autoload:$className | $classId | is already loaded\n"; } else{ print "spl_autoload_ autoload: try to load $className | $classId |\n"; if ( false === include($realPath)) print "spl_autoload_ autoload:$className | $classId | failed to be loaded\n"; else print "spl_autoload_ autoload:$className | $classId | is loaded\n"; } }});
- PHP spl_autoload和class_exsits使用技能
- php中spl_autoload详解
- PHP __autoload与spl_autoload
- PHP autoload和spl_autoload自动加载机制详解
- PHP php中spl_autoload详解
- spl_autoload的一点使用心得
- Php应聘技能要求
- PHP 技能树
- php技能树
- Qt中使用的C++知识和技能
- PHP工程师技能水平区分
- PHP工程师技能水平区分
- php高级工程师必备的技能
- linux vim使用技能
- Android-gridview使用技能
- 日常使用技能
- 硬性技能和软性技能孰轻孰重?
- 开发人员的永久技能和临时技能
- 素数专题
- 你必学知道的Java常考基础面试题
- 一文看懂IC芯片生产流程:从设计到制造与封装
- 电脑双网卡同时上内外网亲自实践教程
- C/CPP点滴积累—数组数据类型
- PHP spl_autoload和class_exsits使用技能
- UIDatePicker(loading...)
- matlab内置函数fitgeotrans与transformPointsForward解析
- c++语言段错误
- 公众平台测试帐号开发全流程第1篇-引言
- vbs 合并 excel 表格
- POJ 3349 Snowflake Snow Snowflakes
- TOEFL(20160123)分析
- R语言学习笔记之统计分析篇