This is how I autoload all the classes in my controllers
folder,
# auto load controller classes
function __autoload($class_name)
{
$filename = 'class_'.strtolower($class_name).'.php';
$file = AP_SITE.'controllers/'.$filename;
if (file_exists($file) == false)
{
return false;
}
include ($file);
}
But I have classes in models
folder as well and I want to autoload them too - what should I do? Should I duplicate the autoload above and just change the path to models/
(but isn't this repetitive??)?
Thanks.
EDIT:
these are my classes file names in the controller folder:
class_controller_base.php
class_factory.php
etc
these are my classes file names in the model folder:
class_model_page.php
class_model_parent.php
etc
this is how I name my controller classes class usually (I use underscores and lowcaps),
class controller_base
{
...
}
class controller_factory
{
...
}
this is how I name my model classes class usually (I use underscores and lowcaps),
class model_page
{
...
}
class model_parent
{
...
}
You should name your classes so the underscore (
_
) translates to the directory separator (/
). A few PHP frameworks do this, such as Zend and Kohana.So, you name your class
Model_Article
and place the file inclasses/model/article.php
and then your autoload does...Also note you can use
spl_autoload_register()
to make any function an autoloading function. It is also more flexible, allowing you to define multiple autoload type functions.Edit
I see you are using
controller_*****
andmodel_*****
as a class naming convention.I read a fantastic article, which suggests an alternative naming convention using php's
namespace
.I love this solution because it doesn't matter where I put my classes. The
__autoload
will find it no matter where it is in my file structure. It also allows me to call my classes whatever I want. I don't need a class naming convention for my code to work.You can, for example, set up your folder structure like:
Your classes can be set up like this:
and:
The autoloader could look like this (or see 'a note on autoloading' at the end):
Then... you can call classes in three ways:
or,
or,
EDIT - a note on autoloading:
My main auto loader looks like this:
This autoloader is a direct 1:1 mapping of class name to directory structure; the namespace is the directory path and the class name is the file name. So the class
application\controllers\Base()
defined above would load the filewww/application/controllers/Base.php
.I put the autoloader into a file, bootstrap.php, which is in my root directory. This can either be included directly, or php.ini can be modified to auto_prepend_file so that it is included automatically on every request.
By using spl_autoload_register you can register multiple autoload functions to load the class files any which way you want. Ie, you could put some or all of your classes in one directory, or you could put some or all of your namespaced classes in the one file. Very flexible :)
I use this. Basically define your folder structure (MVC etc) as a constant in a serialised array. Then call the array in your autoload class. Works efficiently for me.
You could obviously create the folder array using another function but for MVC you may as well type it in manually.
For this to work you need to call your classes ...... class.classname.php
My version of @Mark Eirich answer:
In my case only controller class have the keyword in their name, adapt it for your needs.
Here is my solution,
I'm not sure whether it is the best solution or not but it seems to work perfectly...
What do you think??
Here's what I'd do:
As long you name your files consistently, like
class_controller_*.php
andclass_model_*.php
, this should work fine.