Does CodeIgniter support Namespace?
Namespaces are qualifiers that solve two different problems: They allow for better organization by grouping classes that work together to perform a task. They allow the same name to be used for more than one class.
In the PHP world, namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions: Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications. Learn more. Star 243. Fork 109. Why CodeIgniter?
CodeIgniter has rich set of libraries, which you can find in system/libraries folder but CodeIgniter is not just limited to system libraries, you can create your own libraries too, which can be stored in application/libraries folder. You can create libraries in three ways.
How To Get Namespaces to Work in Codeigniter
Actually, you can get namespaces to work in conjunction to relative paths in your application models. This modification makes loading models much easier and also allows you to have interfaces...
Add this to the end of your application/config/config.php
spl_autoload_extensions('.php'); // Only Autoload PHP Files
spl_autoload_register(function($classname) {
if (strpos($classname,'\\') !== false) {
// Namespaced Classes
$classfile = strtolower(str_replace('\\', '/', $classname));
if ($classname[0] !== '/') {
$classfile = APPPATH.'models/' . $classfile . '.php';
}
require($classfile);
} elseif (strpos($classname, 'interface') !== false) {
// Interfaces
strtolower($classname);
require('application/interfaces/' . $classname . '.php');
}
});
Example Namespaced Class:
<?php
// File: application/models/foo/bar.php
namespace foo;
class Bar extends \CI_Model implements \Awesome_interface {
public $foobar;
public function __construct() {
return parent::__construct();
}
public function getFoobar() {
return $this->foobar;
}
public function setFoobar($val) {
$this->foobar = $val;
}
}
Example Instantiation of Class in Your Code Somewhere:
IMPORTANT NOTE: DO NOT USE BUILT IN CI_Loader ( Ex: $this->load->model(); )
// This will Autoload Your Namespaced Class
$example = new foo\Bar();
or alternatively on top of your PHP class (ex: controller, other model), you can do this...
<?php
...
use foo\Bar as FooBar;
...
// Then you can just do this
$example = new FooBar();
Example of Interface:
<?php
// File: application/interfaces/awesome_interface.php
interface Awesome_interface {
public function getFoobar();
}
Namespace are supported by php and not by the framework (codeigniter in your case). If you use namespaces php version must be >= 5.3.0 Codeigniter dosen`t use namespaces because it is written to support php 4.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With