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
7
  • @Hary There is no error,i am getting a blank page. Commented Dec 7, 2011 at 10:46
  • Call to a member function show_hello_world() on a non-object in C:\wamp\www\project\application\controllers\admin.php on line 13 Commented Dec 7, 2011 at 10:50
  • @Hary echo($this->Pro->show_hello_world()); Commented Dec 7, 2011 at 10:58
  • @DileepDil did you try exactly the whole test case in my answer? (I've updated it a bit) Commented Dec 7, 2011 at 11:04
  • @DamienPirsy yeah i did...now i am downloading codeigniter [fresh one] Commented Dec 7, 2011 at 11:06

4 Answers 4

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();
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you ,but now Undefined property: Admin::$Pro
You need to remove it from the library constructor, not the controller contstructor
define('ENVIRONMENT', 'development'); so its correct...log_threshold is set to 4..
#Damien i removed it from the library constructor.
Try reducing the sample code at minimum, like removing unneeded library calls. Just load yours and see
|
4

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());

6 Comments

Thank you ,actually i got the mistake but you answered it well.
@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.
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 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.
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
|
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.