I wanted to use code like this in my Zend Framework project:
public function indexAction() {
$user 开发者_如何学Go= new User();
$user->name = "Guest";
$user->save();
}
The main thing that is important for me is that the class is called just User
and not App_Model_User
but how could I manage that?
I thought I could add the path to the file User.php
in the model-folder to the include_path:
<?php
class User {
/* Some code */
}
But how could I config the autoloader to load that file/class?
Currently I'm using the autoloader with the appnamespace ("App") to load classes called App_Model_User
which works but the class naming is not the right I think. It should be cleaner and clearer.
I use set_include_path
as follows:
$root = dirname(dirname(__FILE__));
set_include_path(
$root . '/application' . PATH_SEPARATOR
. $root . '/library' . PATH_SEPARATOR
. $root . '/application/models' . PATH_SEPARATOR
. $root . get_include_path()
);
Then the autoloader picks up any models (with any name) which I put in /application/models. The above is in my only public facing script (index.php). This is how I set up the autoloader:
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
The second line will ensure that your models do not have to be explicitly included. See:
http://framework.zend.com/manual/en/zend.loader.autoloader.html
What is your version of PHP? You can use namespaces.
精彩评论