4

I need to load more than 2 libraries in a CI Controller and call their member functions. I've tried the following ways but no use.

1.

    $this->load->library('liba');
    $result = $this->liba->SearchTours($searchParams);
    echo $result;

    $this->load->library('libb');
    $result2 = $this->libb->tourFetch($searchParams);
    echo $result2;

2.

    $this->load->library(array('liba' , 'libb'));

    $result = $this->liba->SearchTours($searchParams);
    echo $result;
    $result2 = $this->libb->tourFetch($searchParams);
    echo $result2;

3.

    $this->load->library('liba');
    $this->load->library('libb');

    $result = $this->liba->SearchTours($searchParams);
    echo $result;
    $result2 = $this->libb->tourFetch($searchParams);
    echo $result2;

4.

    public function __construct(){
      parent::__construct();
      $this->load->library('liba');
      $this->load->library('libb');
    }
  1. I have swapped the order of loading in above 4 cases. Whichever is mentioned second does not load.

In all the cases only the first mentioned library is loaded and this Notice shows for the second one.

A PHP Error was encountered Severity: Notice Message: Undefined property: Unify::$libb Filename: controllers/Unify.php Line Number: 30

I couldn't find any soultion on CI user manual or SO. What am I doing wrong?

enemetch
  • 492
  • 2
  • 8
  • 21
  • 1
    I think the problem in your library libb, not in count of them. It is normal to load some libraries for Codeigniter – splash58 May 06 '15 at 11:41
  • I tried swapping (loading libb first and then liba). libb loads successfully but liba doesn't. – enemetch May 06 '15 at 11:43
  • load this in construct – Abdulla Nilam May 06 '15 at 11:44
  • it is your file controllers/Unify.php , not system, i think – splash58 May 06 '15 at 11:45
  • @Abdulla Thanks for the suggestion. I have tried that too. Problem not solved :(. – enemetch May 06 '15 at 11:50
  • @splash58 I'm not sure what you mean. Unify.php is the controller where the above code is written. – enemetch May 06 '15 at 11:51
  • Show us the code of any library. At least, a construct – splash58 May 06 '15 at 11:56
  • try to call one library at a time and see if it works, I think you have a problem with one library either liba or libb – Nassim May 06 '15 at 11:57
  • The problem at loading moment or while calling property? Put any echo after loading – splash58 May 06 '15 at 11:58
  • @Nassim I tried loading one at a time. Both work perfectly in isolation. – enemetch May 06 '15 at 11:58
  • @splash58 Problem while calling the member function. `Fatal error: Call to a member function tourFetch() on a non-object in blahblah\controllers\Unify.php on line 37` tourFetch() is member function of libb – enemetch May 06 '15 at 12:02
  • I think the error may be found only by reading all your code. I load several libraries in my project, it works – splash58 May 06 '15 at 12:22
  • @splash58 My library files' function I'm calling contain SOAP XML calls to third party APIs.. Two library files, two different SOAP calls to two different APIs. they return strings. – enemetch May 06 '15 at 12:29
  • 1
    ok, if both libraries are calling other controllers , you will have a problem, use $CI =& get_instance(); inside the libraries instead of $this->xxx->yyy() and then call you instance like so : $your_variable = $CI->xxx->yyy() – Nassim May 06 '15 at 12:50
  • @Nassim Ok this might be a problem. Can the library files be controllers?? Because I need to load other libraries in those libraries – enemetch May 06 '15 at 12:56
  • 1
    whenever you use libraries and need a built-in method from CI, use $CI =& get_instance(); and then use it like so $CI->load->helper('url'); $CI->load->library('session'); $CI->config->item('base_url'); etc.... see http://www.codeigniter.com/userguide2/general/creating_libraries.html for more info – Nassim May 06 '15 at 13:36
  • @Nassim I removed the controller extension from the libraries and used get_instance() to load other libraries. Now neither library loads :/ – enemetch May 07 '15 at 07:19
  • did you find a solution for your problem ? – Nassim May 19 '15 at 20:58
  • 1
    Try to use the famous php way and see if it works .. $result_a = new liba() , $result_b = new libb() , then see if it works as you expected , if not then there must a problem with your code – JohnMi May 21 '15 at 23:18
  • @RobyMi Thanks. I have currently moved on to some other project. Will definitely try your suggestion when i get back. Appreciated! – enemetch May 22 '15 at 12:34

3 Answers3

2

load multiple library in CI Just pass in it array

$this->load->library( array('liba', 'libb') );

and this notice because you not include

$CI =& get_instance(); 

inside your controller

Saty
  • 22,443
  • 7
  • 33
  • 51
2

Is the file in your controllers folder. If yes then You might not have properly extended your controller. Like:

myController extends CI_Controller{}

else You need to use $CI =& get_instance(); inside your file and then use

$CI->load->library('your_library');

Edit: NOTE: Check if your library file is capitalized inoyur folder structure like Libb.php

Abhinav
  • 8,028
  • 12
  • 48
  • 89
  • Yes my controller is in controllers folder. I have extended my controller to CI_Controller. And I have also used get_instance(). Failed with or without – enemetch May 06 '15 at 12:18
  • If its in your controllers folder you dont have to user $CI =& get_instance(); you can simply use $this->load->library('xyz') – Abhinav May 06 '15 at 12:19
  • Yes I know that. Like i said. It didnt work wither ways – enemetch May 06 '15 at 12:20
  • All my files are "likethis" throughout. And class names are "Likethis" – enemetch May 06 '15 at 12:27
1

whenever you use libraries and helpers and need a built-in method from CI , use

$CI =& get_instance();

and then use it like so

$CI->load->helper('url');
$CI->load->library('session'); 
$CI->config->item('base_url');  //...etc

see codeigniter.com/userguide2/general/creating_libraries.html for more info


or for a better solution, you can use codeigniter + HMVC to be able to call a controller from another and avoid any conflicts and ease your troubleshooting see

MVC vs HMVC for web application development

Cheers !

Community
  • 1
  • 1
Nassim
  • 2,879
  • 2
  • 37
  • 39
  • Thanks Nassim. I have currently moved on to some other project. Will definitely try your suggestion when i get back. Appreciated! – enemetch May 22 '15 at 12:33
  • Thanks for the response HMVC looks great for my next projects but my current project is in MVC CodeIgniter style. HMVC requires me to change the whole project into modules?? If yes, that is a lot of rework. I just need to be able to call a function each from 5-6 different php files from a controller. The php files are controllers right now but I'm flexible to make it a library etc. Is it possible to use HMVC without much code reorganising? – enemetch Jun 10 '15 at 09:16
  • Alright, I am actually working on big project with codeigniter using just MVC, let me tell you that I was able to move all my code from MVC to HMVC in about 1 hour, because when you move your controllers to modules you can also move their libraries under the same module, so you won't loose anything, au contraire it will only gain flexibility . you can follow this video to get started https://www.youtube.com/watch?v=wDqwXpEQ8Pc – Nassim Jun 10 '15 at 13:47
  • if you are not using codeigniter3 (like me) then following this video you will have an issue but this is how to fix it for codeigniter 2.x http://stackoverflow.com/questions/30554851/code-igniter-modular-extensions-access-level-to-mx-router-set-default-contro – Nassim Jun 10 '15 at 13:49
  • There was a breakthrough in this issue yesterday. The problem was bad output from the libraries. I have fixed that and now I can normally load multiple libraries how I described in the question. However... I had to also use $CI =& getInstance in my libraries so as to use built in functions. So if you post it as the answer (your latest upvoted comment by me on the question) I will accept it. – enemetch Jun 11 '15 at 05:44
  • And thank you so much for HMVC and how to migrate from MVC tutorials.. This is will be really helpful for my next projects! Cheers – enemetch Jun 11 '15 at 05:45