Sarah
Sarah

Reputation: 459

Can't Load Codeigniter Custom Library

Library

class getLang {
   public function getText($en, $ar) {
      $CI =& get_instance();
      $lang = $CI->session->userdata('language');
      if ($lang == 'en') {
         $string = $en;
      } else if ($lang == 'ar') {
         if (!empty($ar)) {
            $string = $ar;   
         } else if (empty ($ar)) {
            $string = $en;
         }
      }
      return $string;
   }
}

Controller

class Home extends MY_Controller {
   public function test() {
      $this->load->library('getLang');
      return $this->getLang->getText('text', '');
   }
}

When I run home/test I get this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Home::$getLang

Filename: controllers/home.php

Line Number: 6

I already loaded the custom library in config/autoload. Looks like the library isn't loaded, I also tried loading it in the controller, still doens't work! How can I solve this?

Upvotes: 0

Views: 4452

Answers (1)

Maxime Morin
Maxime Morin

Reputation: 2008

CodeIgniter's documentation clearly states that class name and file name must be capitalize. From the documentation:

  1. File names must be capitalized. For example: Myclass.php
  2. Class declarations must be capitalized. For example: class Myclass
  3. Class names and file names must match.

That's probably why it isn't correctly loaded.

On a side note, a class named getLang sounds wrong. It sounds like a method. I would suggest a name such as Language, Translator, ...

Edit: The documentation also asks to access your library by using the lower case version of the name. Example: $this->getlang->getText(...)

Upvotes: 9

Related Questions