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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改