Welcome Guest, Not a member yet? Register   Sign In
Use namespaces in Codeigniter 3.1.6
#1

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!!!
Reply
#2

I am interesting to.
Reply
#3

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-n...controller
Reply
#4

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




Theme © iAndrew 2016 - Forum software by © MyBB