Efficient PHP auto-loading and naming strategies -


Like most web developers these days, I am enjoying the benefits of solid MVC architecture for web applications and sites when

I have just become a fan of defining a single more than __ autoload () function, as it is clearly more flexible if you include various base modules However, I think I have never felt good about the loading function that I write. They include many string scans and directory scanning to look for possible sections to load. For example, suppose I have apps like PATH_APP , and model , view and controllers A simple structure with directories named I often use a naming structure through which the files are named IndexView.php and IndexController.php inside the appropriate directory, and by default of any specific scheme of the model is not . If I register with spl_autoload_register this may be a loader function for this structure:

  public function MVCLoader ($ square) {if (file_exists (PATH_APP '/ model /'. $ Class '.php')) {require_once (PATH_APP. '/ Model /'. $ Class '.php'); Back true; } Else if (strpos ($ square, 'view') == incorrect) {if (file_exists (PATH_APP. / Ideas / '. Class 2.' .php ')) {require_once (PATH_APP.' / Ideas / .. $ class '.php'); Back true; }} Else if (strpos ($ class, 'controller')! == incorrect) {(file_exists (PATH_APP. '/ Controllers /'. Class '.php if')) {require_once (PATH_APP. / Controller / '. $ Square.' Php '). Back true; }     }     return false; }  

If it is not found then, then there may be another function for scanning the sub-directory in the Model directory. However, if all / other-ing, string checking and directory scanning are being shown to me, and I want to improve it.

I am very curious to know that file naming and automatic loading strategies employ other developers. I'm especially looking for good techniques for efficient automatic loading, and not the option of autoloading.

I am using it in all my projects (directly from the source of the previous one): < / P> public static function loadClass ($ class) {$ files = array ($ class. '.php', str_replace ('_', '/', $ square). '.php', ); Foreach ($ PATH_SEPARATOR, ini_get ($ include_path 'as base_path))) {foreach ($ files as $ file) {$ path = "$ base_path / $ file"; If (file_exists ($ path) & amp; is_readable ($ path)) {include_once $ path; Return; }}}}

If I include someClass_SeperatedWith_Underscores path to see it will look for someClass_SeperatedWith_Underscores.php followed by someClass / SeperatedWith / Underscores.php currently contained in each directory.

Edit: I wanted to keep it out there that I use it for efficiency in development, and not necessarily time processing. If you have peers on your way, then you can use classes with it and you do not need to include them.

I keep my classes in the hierarchy of directories, breaking underscore namespaces ... this code let me keep the file structure beautiful and clean if I want to, or inject a quick class file without nested directories If I want to (it is the defendant on adding a square or two to a library, but I'm working on this time, not part of the project.)


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -