编码器库-加载自定义类时出错


Codeigniter libraries - Error loading custom classes

我无法加载自定义类,其中从任何核心类扩展。我已经把我的自定义类放在application/libraries的子文件夹中。

这是我的文件夹结构
application
    |_ libraries
      |_ cgh
          |_ cgh_asset.php
          |_ cgh_article.php
          |_ cgh_asettype.php
    |_ controllers
      |_ welcome.php

类Cgh_article是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";
}
?> 
控制器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');
    }
} 

得到的错误是:无法加载请求的类:Cgh_assettype

我必须尝试所有可能的类名,文件名的大小写组合,但错误总是相同的。


在经历了一些答案之后,我想我可能应该在这里添加一个基本问题-是否有可能在codeigniter中拥有我自己的自定义对象类型…从我的问题中应该很明显的类型?


这似乎对我有用,所以这是我将要做的…至少在有东西坏掉之前:

在我的控制器的构造函数中,我使用require_once为我的类…好的是,我可以将所有的类合并到一个文件中——我的类最初是在一个文件中——这是修改后的控制器,这是可行的:

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

您需要为每个库调用$this->load->library

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

$this->load->library有3个参数

  1. 加载
  2. 文件
  3. (可选)$config array
  4. (可选)字符串,将库重命名为($this->Renamed_library)

如果你想在一行中加载多个库,使用数组作为第一个参数。

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

库文件名大写吗?(您提交的文件夹结构说它们不是)。

我不知道子目录中是否有库,但我知道文件名需要大写。

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

命名约定

  • 文件名必须大写。例如:Myclass.php
  • 类声明必须大写。例如:class Myclass
  • 类名和文件名必须匹配

您应该只加载一次库或其他东西。如果你是第二次加载,你会得到这个错误