1

I am unable to load my custom class which doesnot extend from any core class. I have placed my custom class in a subfolder inside application/libraries.

So here is my folder structure

application
    |_ libraries
      |_ cgh
          |_ cgh_asset.php
          |_ cgh_article.php
          |_ cgh_asettype.php
    |_ controllers
      |_ welcome.php

Class Cgh_article is a subclass of Cgh_asset

Cgh_asset.php :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
abstract class Cgh_asset
{
    public $id;
    public $type;
    public $title;
    public $content;
    public $user;

    abstract public function generate_url();
    function __construct()
    {
        $this->generate_url();
    }
}

?> 

Cgh_article.php :

<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Cgh_article extends Cgh_asset
{
    function __construct()
    {
        parent::__construct();
        $this->type=Cgh_assettype::article;
    }
    function generate_url()
    {
        $this->url="Article_URL";
    }
}


?> 

Cgh_assettype.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Cgh_assettype
{
    const type1="type1";
    const type2="type2";
    const article="article";
}

?> 

Controller welcome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {
        $this->load->library('cgh/Cgh_assettype','cgh/Cgh_asset','cgh/Cgh_article');

        $this->load->view('welcome_message');
    }
} 

The error I get is : Unable to load the requested class: Cgh_assettype

I must have tried all possible upper and lower case combinations of classnames, filenames, but the error is always the same.


After going through some answers, I think probably I should add one basic question here - Is it at all possible to have my own custom object types within codeigniter ... types that should be quite obvious from my question ?


This seems to work for me, so here is what I will be doing ... at least till something breaks :

In the constructor of my controller, I use require_once for my classes ... and good thing is I can combine all my classes into a single file -- my classes initially were in a single file anyways --This is my controller after the changes, and this works :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {


    public $cgh_assettype;
    public $cgh_asset;
    public $cgh_article;

    function __construct()
    {
        parent::__construct();
        //$this->load->library(array('cgh/cgh_assettype','cgh/cgh_asset','cgh/cgh_article'));
        echo "Including CGH<br />";
        echo "<p>Apppath is ". APPPATH. "</p>";
        require_once(APPPATH.'libraries/cgh/Cgh_assettype.php');
        require_once(APPPATH.'libraries/cgh/Cgh_asset.php');
        require_once(APPPATH.'libraries/cgh/Cgh_article.php');

    }

    public function index()
    {
        $iCgh_article=new Cgh_article();
        echo "<p>$iCgh_article->url</p>";
        $this->load->view('welcome_message');
    }
}
4
  • Have you tried lowercasing filenames and loading an array of libraries; $this->load->library(array('cgh/cgh_assettype','cgh/cgh_asset','cgh/cgh_article'));? Commented Sep 15, 2011 at 23:10
  • I've tried all possible combinations of lower and uppercase versions of classnames, filenames. Doesnt work. I've tried removing the Cgh_ prefix from classnames, filenames. Doesnt work. I've tried moving my class files into the root of application/libraries. Doesnt work. Currently I am trying require_once(APPPATH.'libraries/cgh/Cgh_assettype.php'); in index.php Commented Sep 15, 2011 at 23:23
  • Btw you are trying to load cgh_assettype but your file is named as cgh_asettype.php, did you check that? Commented Sep 15, 2011 at 23:25
  • I wish that was the case Burak ! The folder structure in this question was simply typed in by me, and that was a typo... But many thanks for the tip ... I re-checked all filenames. Commented Sep 15, 2011 at 23:33

3 Answers 3

1

You need to call $this->load->library for each library.

$this->load->library('cgh/Cgh_assettype');
$this->load->library('cgh/Cgh_asset');
$this->load->library('cgh/Cgh_article');

$this->load->library takes 3 parameters.

  1. File to load
  2. (optional) $config array
  3. (optional) String to rename library to ($this->Renamed_library)

If you want to load multiple libraries on one line, use an array as the 1st parameter.

$this->load->library(array('cgh/Cgh_assettype','cgh/Cgh_asset','cgh/Cgh_article'));
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Rocket. But I tried that, dint work. Even if I load only one of the classes, say $this->load->library('cgh/Cgh_assettype'); it gives an error for Cgh_assettype
Maybe CodeIgniter doesn't like the Cgh_ prefix? Try removing that.
Unfortunately, that dint work either. Another thing I tried is moving these class files from the cgh folder to the root of libraries. Still the error remains...
1

Are you library file names capitalized? (Your submitted folder structure says they are not).

I don't know about having libraries within sub-directories, but I know the file names need to be capitalized.

http://codeigniter.com/user_guide/general/creating_libraries.html

Naming Conventions

  • File names must be capitalized. For example: Myclass.php
  • Class declarations must be capitalized. For example: class Myclass
  • Class names and file names must match.

Comments

0

You should load library or whatever only once. If you're loading for second time, you get that error.

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.