Welcome Guest, Not a member yet? Register   Sign In
Why I can't load classes
#1

[eluser]ReyPM[/eluser]
Hi every:
I have a simple class under /application/libraries called/MY_Usession.php. By default it's enabled in autoload.php as follow:
Code:
$autoload['libraries'] = array('database','session','my_usession','form_validation');
and my config.php is this:
Code:
$config['subclass_prefix'] = 'MY_';
The code in my class is very simple:
Code:
<?php
if (!defined('BASEPATH'))
   exit('No direct script access allowed');

class MY_Usession extends CI_Session {

   public $logged_in = FALSE;

   public function __construct() {
      parent::__construct();
      $this->is_logged_in();
   }

   public function is_logged_in() {
      $logged = $this->userdata('id_usuario');
      $this->logged_in = ($logged) ? TRUE : FALSE;
   }

}
When I access my application under my local server (XAMPP, Windows 7, PHP 5.3.x, Apache2) all works fine but when I try in the hosting the application fails with this error:
Quote:An Error Was Encountered
Unable to load the requested class: my_usession
And I don't know why. The host is under Linux and PHP 5.0.4. Could this be the cause?

Cheers and thanks in advance
#2

[eluser]InsiteFX[/eluser]
Server Requirements
PHP version 5.1.6 or newer.
A Database is required for most web application programming. Current supported databases are MySQL (4.1+), MySQLi, MS SQL, Postgres, Oracle, SQLite, and ODBC.

InsiteFX
#3

[eluser]ReyPM[/eluser]
So there is not way that this could work in PHP 5.0.x?
#4

[eluser]InsiteFX[/eluser]
Nope! Sorry.

Do what I did contact your hosting provider and tell them that you are a PHP developer and that you need the version of PHP ugraded to work with your applications!

If that does not work go into there forums and start complaining about it!

InsiteFX
#5

[eluser]toopay[/eluser]
Before we get into PHP 5.xx requirement, lets TRY to fix the 'Load The Library Problems' first. Because the database IS NOT his issue (yet).

@ReyPM
if your library just do that stuff, i think you dont need to extend CI Session. Create a file, under 'application/library', name it 'Loginsession.php' or whatever you like. Then write your own library. Based by code above, it should be :
Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Loginsession {
   protected $_CI;
   public $logged_in = FALSE;

   public function __construct()
   {
      $this->_CI =& get_instance();
      $this->is_logged_in();
   }

   public function is_logged_in()
   {
      $this->_CI->load->library('session');
      $logged = $this->_CI->session->userdata('id_usuario');
      $this->logged_in = ($logged) ? TRUE : FALSE;
   }

}
Then, if you want to auto load it, put it like you load general library
Code:
$autoload['libraries'] = array('database','session','loginsession','form_validation');
Try it, and let's see if it can work Smile
#6

[eluser]Nick_MyShuitings[/eluser]
I actually had a similar issue with a coworker, it was more related to the underscore and Uppercase letters following the underscore. His library name DB_Logger would not work, but renaming to DB_logger fixed it.

Try leaving your code identical and making a single simple class name and file name with no underscore. If that solves your problem you can rule out errors within your class and its just an issue of how windows versus unix systems deal with case sensitive file names.
#7

[eluser]henry178[/eluser]
I have the same problem with PHP Version 5.2.4

Any solutions?
#8

[eluser]Frank Wong[/eluser]
I just ran into this also and it is a strange behavior.

Just to rule out the "you don't have the required version argument".
<b>System:</b>
Ubuntu
php 5.3.2
CI 2.0.2

application/libraries/My_Test.php
Code:
class My_Test {

    function show_form()
    {
        echo 'success';
    }
}

In a controller
Code:
$this->load->library('My_Test'); //works
$this->load->library('my_Test'); //works
$this->load->library('my_test'); //does not work
$this->load->library('mY_Test'); //does not work
$this->load->library('my_TeSt'); //does not work

The first character is case insensitive. All other characters are case sensitive.
But to be safe, just make sure your load statement uses the exact case of your class declaration to avoid any issues.
#9

[eluser]danmontgomery[/eluser]
Classes should have the first letter of the class name capitalized, and all other letters lowercase. The only time you capitalize a letter other than the first is when you're extending a core class. If you follow the naming convention correctly, casing in your call to load() is irrelevant.
#10

[eluser]Aken[/eluser]
To the original poster: Your problem is you are renaming a library you are trying to extend. If you want to extend the Session library, name your new file and class MY_Session, and load it with $this->load->library('session'); It is not a PHP version issue (though you will likely run into PHP version-related issues in other parts of CI, so you should still definitely upgrade).

There are recommended naming conventions you should use in CodeIgniter.

First, do not use the MY_ prefix unless you are extending a core or library class. It's just simpler that way (dig through the Loader library if you want to know why).

Second, the problems Frank has experimented with are related to case sensitivity in file names. When CI attempts to find a library file name, it searches for both ucfirst($class) then strtolower($class) versions. In his test, the first two working attempts resulted in a correct filename of My_Test.php, where the rest all had differences in case at some point.

What I do, and what I recommend:

- File names should consist of the MY_ prefix when extending a class, then a single capital letter and all lowercase / underscores.
Code:
MY_Session.php
MY_Form_validation.php
Users.php
Smarty_parser.php

- Class names should match the file name (for consistency). PHP is less anal about case sensitivity when it comes to loading class names, but it's still better.

- Load classes either fully lowercase or exactly matching the file name, without the MY_ prefix.
Code:
$this->library->load('session');
$this->library->load('Form_validation');
$this->library->load('Users');
$this->library->load('smarty_parser');




Theme © iAndrew 2016 - Forum software by © MyBB