Home > Backend Development > PHP Tutorial > Can CodeIgniter use namespaces?

Can CodeIgniter use namespaces?

WBOY
Release: 2016-08-18 09:15:52
Original
2442 people have browsed it

There seems to be no namespace section in the CodeIgniter document.
PS: How to use libraries loaded by composer in CodeIgniter.

Reply content:

There seems to be no namespace section in the CodeIgniter document.
PS: How to use libraries loaded by composer in CodeIgniter.

CI3 begins to support namespaces.
If you want to use Composer you can change $config['composer_autoload'] = TRUE in the config/config.php file;this option can be changed to TRUE, the default is FALSE

Pay attention here. If you change it to TRUE, then CI will automatically load application/vendor/autoload.phpSo, if your vendor directory is in the root directory of your project, that is, at the same level as index.php, then you You can use $config['composer_autoload'] = realpath(APPPATH . '../vendor/autoload.php'); to introduce Composer

No namespace means that the CI framework cannot use the namespace.
But you can use your own code.
If you want to use composer, you can add

where you want to use the composer library
<code>require __DIR__.'/vendor/autoload.php';
...</code>
Copy after login

Remember to adjust the path yourself

PHP versions greater than 5.3 support namespaces, which has nothing to do with the framework.
You only need to use the usekeyword

before the class definition.
<code class="php">//use 你的composer包的完全限定名

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class TestController {
    
}</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template