Created
May 22, 2014 07:36
-
-
Save JunaidQadirB/7533af3b9b5fd078910d to your computer and use it in GitHub Desktop.
Enable Class loading with Namespaces in CodeIgniter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Append this to the end of your application/config.php | |
* @see http://stackoverflow.com/questions/3700626/namespace-in-php-codeigniter-framework#21858556 | |
*/ | |
spl_autoload_extensions('.php'); // Only Autoload PHP Files | |
spl_autoload_register(function($classname) { | |
if (strpos($classname, '\\') !== false) { | |
// Namespaced Classes | |
$classfile = (str_replace('\\', '/', $classname)); | |
if ($classname[0] !== '/') { | |
$classfile = APPPATH . 'libraries/' . $classfile . '.php'; | |
} | |
require($classfile); | |
} else if (strpos($classname, 'interface') !== false) { | |
// Interfaces | |
strtolower($classname); | |
require('application/interfaces/' . $classname . '.php'); | |
} | |
}); |
I'd like to note that you can always register your namespaces and load them via composer. CodeIgniter in version 3.x specifically allows this via configuration.
Hi,
Is this effect all my .php which I do not wish to add as a namespace or only namespaced one file will affect?
Worked for me. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can i also give namespace to controllers in CI 2.0.4