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
{
}
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:
The class that is extended by the helper is autoloaded and putting it in
require_once
does nothing.Generally speaking, you can avoid
require_once
calls almost entirely by appropriately usingZend_Loader_Autoloader
. Of course, the key is "appropriate".Typically, your
public/index.php
sets theinclude_path
to be thelibrary
folder. Then, if you are usingZend_Application
, theZend_Loader_Autoloader
is registered to find any PSR-0 compliant classes whose namespace prefixes have been registered using theautoloadernamespaces
array inapplication/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 inapplication/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 extendingZend_Application_Bootstrap_Bootstrap
and module bootstraps that extendZend_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.