开发者

Codeigniter libraries - Error loading custom classes

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 "<开发者_JS百科;p>$iCgh_article->url</p>";
        $this->load->view('welcome_message');
    }
}


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜