设为首页 收藏本站
查看: 1352|回复: 0

[经验分享] php的spl_autoload_register与zend framework的Zend_Loader_Autoloader

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-27 08:39:35 | 显示全部楼层 |阅读模式
  初初使用php的时候,引用其他类,都是首先获取类的局对路径,然后通过require_once导入。
DSC0000.png
  那么在child继承parents之前,先导入parents.php



<?php
require_once realpath(dirname(dirname(__FILE__)).'/PParent.php');
class PSon extends PParent
{
    function __construct(){
   
    }
}  这样做,对于类结构简单的情况,发现不出来问题(由于当初需要速成php,完成外接的项目,所以就这样做了)
  当一个php文件中需要引用的类非常多的时候,而且每次编译的时候,用不了几个类,还用上述的方法,就会导致大部分的文件导入时多余的,所以php的autoload功能(动态加载php文件)就应运而生了。
  今天刚学习了这个,现在记录一下。由于我做项目用的是zend_framework,所以同时分析了zend_framework中封装的zend_loader_autoloader的实现。
  首先是php5的API----》spl_autoload_register(使用于(PHP 5 >= 5.1.2))
  使用的步骤如下:
  1、定义autoload方法(方法名自定义就可以);
  2、用spl_autoload_register注册autoload方法(可以注册多个autoload方法);
  3、使用需要的类(注册autoload方法之后,调用类的时候,会自动按照autoload方法的注册顺序进行load,如果load成功,那么停止执行下一个autoload方法,否则一直到所有autoload方法执行完为止)
  sample:
  文件路径:
DSC0001.png
  代码:



<?php
class PParent
{
    function __construct(){
   
    }
}


<?php
class PSon extends PParent
{
    function __construct(){
   
    }
}


<?php
class PDaughter extends PParent
{
    function __construct(){
   
    }
}


<?php
global $fileDir;
$fileDir = dirname(__FILE__);
class autoloader
{
    public static function parentLoad($class)
    {
        global $fileDir;
        $classDir = realpath($fileDir . './model/' . $class . '.php');    //获得类的路径
        if (file_exists($classDir))
        {
            require_once $classDir;        
            return true;
        }
        else {
            return false;
        }
    }
    public static function childLoad($class)
    {
        global $fileDir;
        $classDir = realpath($fileDir . './model/child/' . $class . '.php');//获得类的路径
        if (file_exists($classDir))
        {
            require_once $classDir;        
            return true;
        }
        else {
            return false;
        }
    }
}
spl_autoload_register('autoloader::parentLoad');    //注册自动加载的方法
spl_autoload_register('autoloader::childLoad');
$par = new PParent();//使用类,前面没有显示的调用Model_PParent哦
$child = new PSon();
$daughter = new PDaughter();  zend framework对php5的autoload进行了封装,其实是对类的命名进行了规范,并实现了对应的autoload方法
  现在按照zend_loader_autoloader的规范,对上面的程序进行改造。
  步骤:
  1、将zend framework的library导入项目(等会说导到哪个目录);
  2、改类名,如A/B/C.php,则类名改成A_B_C;
  3、获取zend_loader_autoloader单例,并设置namespace;
  4、使用需要的类;
  项目路径:
DSC0002.png
  代码:



<?php
class Model_Child_PDaughter extends Model_PParent
{
    function __construct(){
   
    }
}


<?php
class Model_Child_PSon extends Model_PParent
{
    function __construct(){
   
    }
}


<?php
class Model_PParent
{
    function __construct(){
   
    }
}


<?php
global $fileDir;
$fileDir = dirname(__FILE__);
$oldIncludePath = get_include_path();//获取旧的环境路径
$newIncludePath = implode(
    PATH_SEPARATOR,
    array(
           realpath($fileDir),
           realpath($fileDir . '/library/'),
        $oldIncludePath,
    )
);//新的环境路径
set_include_path($newIncludePath);//设置新的环境路径

require_once $fileDir.'/library/zend/loader/Autoloader.php';//引入zend_loader_autoloader

$loader = Zend_Loader_Autoloader::getInstance();//获取单例
$loader->registerNamespace('Model_');//注册命名空间
/**
* 上面的$newIncludePath包含了 项目根目录$fileDir 与 项目下的library目录
* 是由于我用的
* zend_loader_autoloader的命名空间Zend_(默认的命名空间有Zend_和Zendx_)是从library开始,
* 我自定义的命名空间Model_是从 项目根目录$fileDir开始。
*
*  注意:命名空间的区分大小写,需要与类定义的大小写对应,如Model_PParent,命名空间就必须定义为Model_
*/

$par = new Model_PParent();//使用类,前面没有显示的调用Model_PParent哦
$child = new Model_Child_PSon();
$daughter = new Model_Child_PDaughter();  总结:
  使用spl_autoload_register与zend_loader_autoloader不矛盾,因为zend_loader_autoloader也是对spl_autoload_register进行封装,总之,它们还是按照注册的顺序进行加载的。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-104749-1-1.html 上篇帖子: PHP中如何保持SESSION不过期,原理及方案 下篇帖子: PHP多进程处理并行处理任务实例
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表