Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Simple Codeigniter library

For my current project i decided to create a library for some common functionalities.

Ex : Login_check,get_current_user etc.

With my little knowledge i created a simple one but unfortunately its not working.

Here my library :

FileName : Pro.php and located in application/libraries

class Pro{

    public function __construct()
    {

       parent::_construct();
        $CI =& get_instance();
       $CI->load->helper('url');
       $CI->load->library('session');
       $CI->load->database();
    }

    function show_hello_world()
    {
        $text = "Hello World";
        return $text;
    }
}

?> 

And i tried to load it on my controller :

<?php
class Admin extends CI_Controller
{
    function __construct()
    {     
        parent::__construct();
        $this->load->database();
        $this->load->library(array('session'));
        $this->load->library("Pro");
    }
    function index()
    {
        echo($this->Pro->show_hello_world());
    }
}

?>

I cant see any erros there...but i am getting a blank page.

Whats wrong with me ??

Thank you .

Edit : I got this error :

Call to a member function show_hello_world() on a non-object in C:\wamp\www\Project\application\controllers\admin.php on line 13
like image 311
Red Avatar asked Dec 07 '11 10:12

Red


People also ask

What is a library in CodeIgniter?

The essential part of a CodeIgniter framework is its libraries. It provides a rich set of libraries, which indirectly increase the speed of developing an application. The system library is located at system/libraries. All we need to do is to load the library that we want to use.

What is the difference between helper and library in CodeIgniter?

The main difference between Helper and Library in CodeIgniter is that the Helper is a file with a set of functions in a particular category and is not written in Object Oriented format, while the Library is a class with a set of functions that allows creating an instance of that class and is written Object Oriented ...

Where is a newly created library stored in CodeIgniter structure?

Storage. Your library classes should be placed within your application/libraries directory, as this is where CodeIgniter will look for them when they are initialized.

Which function is used to load a library in CodeIgniter?

You will load it using: $this->load->library('flavors/chocolate'); You may nest the file in as many subdirectories as you want. Additionally, multiple libraries can be loaded at the same time by passing an array of libraries to the load function.


1 Answers

One thing I notice: remove the parent::__construct() from your library constructor, because it's not extending anything so has no parent to call.

Also, enable error reporting by setting the environment to "development" in index.php, and you might also want to raise the logging threshold to 4 in config/config.php so you log errors.

Try this simple test-case:

file Pro.php in application/libraries:

class Pro {

  function show_hello_world()
  {
    return 'Hello World';
  }
}

Controller admin.php in application/controllers

class Admin extends CI_Controller
{
    function index()
    {
        $this->load->library('pro');
        echo $this->pro->show_hello_world();
    }
}
like image 129
Damien Pirsy Avatar answered Sep 29 '22 10:09

Damien Pirsy