I have seen these,
How to autoload class with a different filename? PHP
Load a class with a different name than the one passed to the autoloader as argument
I can change but in my MV* structure I have:
/models
customer.class.php
order.class.php
/controllers
customer.controller.php
order.controller.php
/views
...
In the actually classes they are,
class CustomerController {}
class OrderController{}
class CustomerModel{}
class OrderModel{}
I was trying to be consistent with the names. If I do not put the class name suffix (Controller, Model), I cannot load the class because that is redeclaring.
If I keep the names of my classes, autoload fails because it will look for a class file named
CustomerController
when the file name is really,
customer.controller.php
Are my only ways to (in no order):
- use create_alias
- rename my files (customer.model.php to customermodel.php)
- rename my classes
- use regular expressions
- use a bootstrap with included files (
include
,require_once
, etc.)
?
Example code,
function model_autoloader($class) {
include MODEL_PATH . $class . '.model.php';
}
spl_autoload_register('model_autoloader');
It seems I have to rename files,
http://www.php-fig.org/psr/psr-4/
"The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name."
Looks to me this can be handled with some basic string manipulation and some conventions.
The output is
You now need to use
makeFileName
inside the autoloader function.I myself am strongly against stuff like this. I'd use namespaces and file names that reflect the namespace and class name. I'd also use Composer.
(I found
splitCamelCase
here.)