CodeIgniter Forums
Use namespaces in Codeigniter 3.1.6 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Use namespaces in Codeigniter 3.1.6 (/showthread.php?tid=69433)



Use namespaces in Codeigniter 3.1.6 - blackiven - 11-22-2017

Hi, I want to use my own classes but it throws me an type error.

Type: TypeError

Message: Argument 1 passed to Posts::__construct() must be an instance of PostsRepository, none given, called in C:\wamp64\www\ciblog\system\core\CodeIgniter.php on line 518



Here is my code:

Posts.php

PHP Code:
class Posts extends CI_controller
{
    
    public 
function __construct (\application\repositories\posts\PostsRepository $postsRepository) {
      $this->postsRepository $postsRepository;

    }

...... 



PostsRepository.php


PHP Code:
<?php

class PostsRepository() {
    
    public 
function all(){

        return \application\models\Post_model->get_posts();

    }



How can I solve this problem? Thank you!!!


RE: Use namespaces in Codeigniter 3.1.6 - bvrignaud - 11-22-2017

I am interesting to.


RE: Use namespaces in Codeigniter 3.1.6 - orionstar - 11-22-2017

AFAIK if you use the offical package of CI without extensions you cannot use namespaces with the main classes (controllers, models, libraries).
If you must use namespaced classes as controllers you have to redefine the loader or the CI base class.
You can find a possible implementation here: https://github.com/kenjis/codeigniter3-namespaced-controller


RE: Use namespaces in Codeigniter 3.1.6 - blackiven - 11-22-2017

Thanks. In my case I want to use Dependency Injection.