• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem while adding Custom Libraries to CI3

#1
HI,
I have a working application in previous version of CI and is working well, i am trying to create a new application with somewhat reuse of previous code, (but it's a new application, not an upgrade),

I downloaded the latest zip of CI from website.
I setup CI and it worked well, i was able to see the welcome controller,
I copied some of my previous helping libraries into libraries directory and included them
as
Code:
$this->load->library('directoryinfo');
and couple of other libraries also the same way in a function loads(), which is called in the constructor of the controller,
but this give me the following error

PHP Code:
A PHP Error was encountered

Severity
Error

Message
: Class 'CI_Directoryinfo' not found

Filename
core/Common.php

Line Number
192 

I am not able to understand , why i am receiving this error, because, in my class there is no "CI_ " in the name.

So i need help in this regard

Thanks
Reply

#2
What exactly are the class name and its file name?
Reply

#3
(07-02-2015, 06:27 AM)Narf Wrote: What exactly are the class name and its file name?
Class name as mentioned below

Code:
class Directoryinfo {


    public function __construct()
    {
    }
while file name is also Directoryinfo.php
Reply

#4
OK, then move it out of system/libraries/ and put it in application/libraries/ instead ... you're not supposed to touch the system/ directory.
Reply

#5
(07-03-2015, 01:24 AM)Narf Wrote: OK, then move it out of system/libraries/ and put it in application/libraries/ instead ... you're not supposed to touch  the system/ directory
this library is present inside application/libraries directory , i haven't touched the system/ directory, instead i have checked the common.php and found the code below and you can see that "CI_' is being concatenated with every class name whether it exists in /system/ or /application/
PHP Code:
function &load_class($class$directory 'libraries'$param NULL)
    {
        static 
$_classes = array();
        
// Does the class exist? If so, we're done...
        
if (isset($_classes[$class]))
        {
            return 
$_classes[$class];
        }

        
$name FALSE;

        
// Look for the class first in the local application/libraries folder
        // then in the native system/libraries folder
        
foreach (array(APPPATHBASEPATH) as $path)
        {
            if (
file_exists($path.$directory.'/'.$class.'.php'))
            {
                
$name 'CI_'.$class;
                if (
class_exists($nameFALSE) === FALSE)
                {
                    require_once(
$path.$directory.'/'.$class.'.php');
                }

                break;
            }
        }
        
// Is the request a class extension? If so we load it too
        
if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
        {
            
$name config_item('subclass_prefix').$class;

            if (
class_exists($nameFALSE) === FALSE)
            {
                require_once(
APPPATH.$directory.'/'.$name.'.php');
            }
        }

        
// Did we find the class?
        
if ($name === FALSE)
        {
            
// Note: We use exit() rather then show_error() in order to avoid a
            // self-referencing loop with the Exceptions class
            
set_status_header(503);
            echo 
'Unable to locate the specified class: '.$class.'.php';
            exit(
5); // EXIT_UNK_CLASS
        
}

        
// Keep track of what we just loaded
        
is_loaded($class);
        
$_classes[$class] = isset($param)
            ? new 
$name($param)
            : new 
$name();
        return 
$_classes[$class];
    }


don't know if there is a fix in the core files, or i am doing something wrong, but the same libraries in the same way worked with previous version and not working on this CI3
Reply

#6
Well, neither CI_Loader::library(), nor any method in its stack trace call the load_class() function. And also, line 192 in system/core/Common.php for CI3 is this:

Code:
is_loaded($class);

... which couldn't possibly trigger the error message that you originally reported.

So, you most certainly have made modifications to the system/ directory, even if you didn't put your custom libraries in there. And that doesn't exclude the possibility of you having a MY_Loader override or one of your custom libs calling load_class() itself.
Reply

#7
(07-03-2015, 02:53 AM)Narf Wrote: Well, neither CI_Loader::library(), nor any method in its stack trace call the load_class() function. And also, line 192 in system/core/Common.php for CI3 is this:


Code:
is_loaded($class);

... which couldn't possibly trigger the error message that you originally reported.

So, you most certainly have made modifications to the system/ directory, even if you didn't put your custom libraries in there. And that doesn't exclude the possibility of you having a MY_Loader override or one of your custom libs calling load_class() itself.

i put some echo in the common.php to check the error and what's happening there, so line number might change, as at my side line 192 points to 
PHP Code:
$_classes[$class] = isset($param)

 ? new 
$name($param)
: new 
$name(); 

and the error is due to the
Code:
new $name()
part, because it is trying to initialize a class CI_Directoryinfo , while it's not present there, because the class name of the file included above is Directoryinfo only, and i didn't made any changes to core , i just downloaded the CI3 zip file and started adding my libraries and controllers from the previous application.

Also if you might have noticed the following part in the above function that i pasted from Common.php CI_ is being added to all class names whether it's in system or application directory
Code:
$name = 'CI_'.$class;
PHP Code:
if (file_exists($path.$directory.'/'.$class.'.php'))
            {
                
$name 'CI_'.$class;
                if (
class_exists($nameFALSE) === FALSE)
                {
                    require_once(
$path.$directory.'/'.$class.'.php');
                }

                break;
            } 

and that will make the variable $name to be having value "CI_libraryname" , so that at the initialization point it will always try to initiate the $name(), while that class doesn't exist, due to the addition of "CI_" in the class name
Reply

#8
I'm very well aware of what load_class() does ... the problem isn't in load_class(), it's that you're calling it from somewhere. And as I said:

(07-03-2015, 02:53 AM)Narf Wrote: neither CI_Loader::library(), nor any method in its stack trace call the load_class() function

Here's proof if you don't believe me:

Quote:$ git checkout 3.0.0
Note: checking out '3.0.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

 git checkout -b new_branch_name

HEAD is now at 3fe7949... [ci skip] Missing composer.json
$ grep 'load_class' system/core/Loader.php
load_class('Model', 'core');
$
Reply

#9
(07-03-2015, 03:34 AM)Narf Wrote: I'm very well aware of what load_class() does ... the problem isn't in load_class(), it's that you're calling it from somewhere. And as I said:


(07-03-2015, 02:53 AM)Narf Wrote: neither CI_Loader::library(), nor any method in its stack trace call the load_class() function

Here's proof if you don't believe me:


Quote:$ git checkout 3.0.0
Note: checking out '3.0.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

 git checkout -b new_branch_name

HEAD is now at 3fe7949... [ci skip] Missing composer.json
$ grep 'load_class' system/core/Loader.php
load_class('Model', 'core');
$

please is there a fix for this unwanted update? i use this feature to share same library with multiple codeigniter installation with one system.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.