6

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
Red
  • 6,230
  • 12
  • 65
  • 112

4 Answers4

16

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();
    }
}
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • Thank you ,but now `Undefined property: Admin::$Pro` – Red Dec 07 '11 at 10:42
  • You need to remove it from the library constructor, not the controller contstructor – Damien Pirsy Dec 07 '11 at 10:43
  • `define('ENVIRONMENT', 'development');` so its correct...`log_threshold` is set to 4.. – Red Dec 07 '11 at 10:44
  • #Damien i removed it from the library constructor. – Red Dec 07 '11 at 10:45
  • Try reducing the sample code at minimum, like removing unneeded library calls. Just load yours and see – Damien Pirsy Dec 07 '11 at 10:49
  • Call to a member function show_hello_world() on a non-object in C:\wamp\www\project\application\controllers\admin.php on line 9 – Red Dec 07 '11 at 10:54
  • Hey, I got question. If I want to do $a = $this->db->where('x1', '123'); $b = $a->where('x2', '456'); ..... Both $a and $b are doing same query, how I prevent that on my library? – Anggie Aziz May 21 '15 at 09:26
3

while your class name is capitalized, all your references to the library when loading it and using it should be lower case. you also do not need the constructor, as the other commenter mentioned.

so instead of:

echo($this->Pro->show_hello_world());

you should have:

echo($this->pro->show_hello_world());
davidethell
  • 11,708
  • 6
  • 43
  • 63
  • Thank you ,actually i got the mistake but you answered it well. – Red Dec 07 '11 at 11:12
  • 2
    @DileepDil I see you DIND'T DO as I told you, since if you read my answer well instead of just applying a bit of this and a bit of that you'd have seen that I'd been using the EXACT SAME THING as this answer all the time. Oh well. – Damien Pirsy Dec 07 '11 at 11:14
  • very true, @DamienPirsy got this one before i did. i didn't see it because he hadn't mentioned it explicitly, yet you can see the lower case in his answer. – davidethell Dec 07 '11 at 11:17
  • @davidethell don't misunderstand me, I don't want to deprive you of the accepted answer mark, I don't care about that :), it just gets on my nerves when someone says he does something while instead still doing his way; I've seen this quite a lot. Dileep, keep this as the accepted answer, just next time pay attention to people that spent 30 minutes trying to figure out what's wrong IN YOUR CODE. – Damien Pirsy Dec 07 '11 at 11:20
  • @DamienPirsy ahh no bro...actually i know this well...and i really appreciated your efforts..the problem is i am typing it lower case but my IDE converting it to Uppercase..so i didnt figured what is wrong with this..so that time i downloaded latest version and just checked ma codes and realized that its in UpperCase ,on that time davidethell answerd it..so i just put it as answer.I am extremely sorry Brother.You are the quality. – Red Dec 07 '11 at 11:28
  • 2
    No problem; you needn't remove this as accepted, I don't care about that, don't get me wrong. IDE usually change that if you accept autocomplete. Anyway, glad you solved your problem, cheers – Damien Pirsy Dec 07 '11 at 11:38
1

I prefer the standard php autoloader approach, with this you dont need to change your classes at all, you can use your standard classes without modifications

say for instance you class is class 'Custom_Example_Example2' and is stored in libraries in sub folders you can add this autoloader in the master index.php

make sure it is added below the defined APPPATH constant

//autoload custom classes
function __autoload($className) {
if (strlen(strstr($className, 'Custom_')) > 0 ||
   strlen(strstr($className, 'Other1_')) > 0 ||
   strlen(strstr($className, 'Other2_')) > 0) {

    $exp  = explode('_', $className);
    $file = APPPATH.'libraries';

    if(!empty($exp)) {
        foreach($exp as $segment) {
            $file .= '/'.strtolower($segment);
        }
    }
    $file .= '.php';
    require_once $file;

    //debug
    //echo $file.'<br />';
}
}

This will look for class calls matching the 'Custom_' prefix and reroute them to the relative location in this case

you only need to define the base prefix not the sub folders / classes these will be auto detected by this code

APPPATH.'libraries/custom/example/example2.php'

You can call it in the controller the standard php way

$class = new Custom_Example_Example2;

or

$class = new custom_example_example2();

You can modify the script to your liking currently it expects all folders and filenames in the library to be lowercase but you can remove the strtolower() function to allow multiple casing.

you can change the require once to echo to test the output by uncommenting this line and refresh the page, make sure you have a class init / test in the controller or model to run the test

echo $file.'<br />';

Thanks Daniel

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
centralhubb.com
  • 2,705
  • 19
  • 17
0

In Pro.php

class Pro{
    protected $CI;
    public function __construct() {
        $this->CI = & get_instance();
    }

    public function showHelloWorld(){
        return "Hello World";
    }
}

In your controller

class Staff extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->database();

        $this->load->helper(array('url_helper', 'url'));
        $this->load->library("pro");

    }

    public function index() {
        echo $this->pro->showHelloWorld();die;

    }
}

Just do these things you can access your custom library in codeignitor.