duanjue7745 2012-05-07 16:12
浏览 49
已采纳

在zend应用程序中动态定义PHP类

Within a Zend app, is there a way to define a class which does not conform to autoloading naming conventions and still have it recognized when I try to use it later on? I imagine you define the class and register it, somehow, with the autoloader, but after reading the autoloader manpage, I still don't understand how to do it.

My use case is during testing (and no, I don't want a mock or stub in this case). I define a new class at the top of my test script, but when my application code references that class, the autolader aggressively tires to load it based on naming convention.

<?php 

class Non_Conforming_Dummy_Class 
{
}

// should I register this new class with the autoloader right here? 

class Whatever
{
public function useIt()
    {
        $class = new Non_Conforming_Dummy_Class;
    }
}

I get a 'no such file or directory' which looks for Non/Conforming/Dummy/Class.php

  • 写回答

2条回答 默认 最新

  • duancan2539 2012-05-07 16:20
    关注

    The ZF Autoloader will load any classes following PSR-0 convention. Any classes not following that convention cannot be loaded with the Autoloader. However, you can

    register arbitrary autoloader callbacks, optionally with a specific namespace (or group of namespaces). Zend_Loader_Autoloader will attempt to match these first before using its internal autoloading mechanism.

    This means, you can add your own autoloader that knows your naming scheme to ZF's autoloader:

    $autoloader->pushAutoloader(array('myAutoloader', 'autoload'), 'MyPrefix');
    

    See http://framework.zend.com/manual/en/zend.loader.autoloader.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程