ZF Include path

2019-02-20 15:05发布

问题:

Is it correct to require_once? where and how would you put it include path?

Should it not be in a application.ini or bootstrap?

EXAMPLE:

require_once 'Zend/View/Helper/Abstract.php';
// @question - is this correct - where and
// how would you put it include path

class Zend_View_Helper_Translate extends Zend_View_Helper_Abstract
{
}

回答1:

Generally speaking, you can avoid require_once calls almost entirely by appropriately using Zend_Loader_Autoloader. Of course, the key is "appropriate".

Typically, your public/index.php sets the include_path to be the library folder. Then, if you are using Zend_Application, the Zend_Loader_Autoloader is registered to find any PSR-0 compliant classes whose namespace prefixes have been registered using the autoloadernamespaces array in application/configs/application.ini.

The tricky part is for classes defined in files that don't "reside on the include_path", like models that appear in application/models, services that reside in application/services, etc. Even though the classes defined there tend to follow PSR-0 standards, the fact that the PSR-0 mapping occurs relative to a base off the include-path means that the system has to know the mapping between classname prefixes and base paths. This is where resource autoloaders come in. These resource autoloaders are typically set up automatically in the application Bootstrap extending Zend_Application_Bootstrap_Bootstrap and module bootstraps that extend Zend_Application_Module_Bootstrap.

View helpers are another example of classes that reside "off the include_path", perhaps in something like application/views/helpers. Since these are typically invoked in a view script using a short form $this->someHelper($someParam), the system must be told how to generate the fully qualified classname from this short name. This is accomplished using $view->addPrefixPath() which maps namespace prefixes to filesystem locations. Again, the app-level and module level bootstrapping mechanism sets most of these up for you.

For libraries/classes that do not follow PSR-0 standards, you can create custom autoloaders and attach them (typically at Bootstrap) to the Zend_Loader_Autoloader singleton. This is the only place where you would have an explicit include/require.

tl;dr: With proper use of the existing ZF autoloader mechanism, you almost never need to have include/require statements in your own application code.



回答2:

It is not correct in this case.

First off, please use Zend Tool. It will create the files you don't know how to create yourself. It will create the correct class names, extend them appropriately and require_once anything that might be needed.

Do not place require_once in the bootstrap. You want it to execute only when needed, not with every request.

As for the example you've provided, the correct version would be:

require_once "Zend/View/Interface.php";
class Zend_View_Helper_Foo extends Zend_View_Helper_Abstract {
}

The class that is extended by the helper is autoloaded and putting it in require_once does nothing.