0

First of all, I'm typing this on a cellphone. So I'm very sorry if I can't use the coding indentation.

Say I have a custom library named XYZ and it is under /library/.

I can add this to the application.INI as autoloaderNameSpaces [] = "XYZ"

Now I have a class Example.php under this folder /library/XYZ/fld1/fld2/fld3/. I know I can call it by using XYZ_fld1_fld2_fld3_Example.php

But how do I define a shortened namespace, for instance, "Short" so I can call this file by using Short_Example.php

Thanks and sorry again for bad notation.

4

1 回答 1

0

要执行此功能,您必须使用插件加载器。使用插件加载器加载类。

这是一个加载器类,用于在 /library/Xyz/Fld1/Fld2/Fld3/ 目录中加载自定义类

这是一个代码示例。

<?php
class Xyz_Core
{
  /**
   * File name Core.php inside Xyz directory
   * 
   * Loader for parsers
   * 
   * @var Zend_Loader_PluginLoader
   */
  protected $_pluginLoader;


  /**
   * Gets the plugin loader
   * 
   * @return Zend_Loader_PluginLoader
   */
  public function getPluginLoader()
  {
    if( null === $this->_pluginLoader )
    {
      $this->_pluginLoader = new Zend_Loader_PluginLoader(array(
        'Xyz_Fld1_Fld2_Fld3_' => 'XYZ/fld1/fld2/fld3/'
      ));
    }

    return $this->_pluginLoader;
  }

  /**
   * Get a helper
   * 
   * @param string $name
   */
  public function getHelper($name)
  {
    $name = $this->_normalizeHelperName($name);
    if( !isset($this->_helpers[$name]) )
    {
      $helper = $this->getPluginLoader()->load($name);
      $this->_helpers[$name] = new $helper;
    }

    return $this->_helpers[$name];
  }

  /**
   * Normalize helper name
   * 
   * @param string $name
   * @return string
   */
  protected function _normalizeHelperName($name)
  {
    $name = preg_replace('/[^A-Za-z0-9]/', '', $name);
    //$name = strtolower($name);
    $name = ucfirst($name);
    return $name;
  }
}

$api = new Xyz_Core();
/*
 * To load object of class Example.php
 */
$obj = $api->getHelper('Example');
/*
 * Or To load include the file only of class Example.php
 */
$class = $api->getPluginLoader()->load('Example');
$obj = new $class($param1, $param2, $etc);
于 2013-08-30T05:05:05.620 回答