2011-09-15 72 views
1

我無法裝入我的自定義類亙古不變的從任何核心類擴展。 我已經把我的自定義類裏面的應用程序/庫的子文件夾。笨庫 - 錯誤加載自定義類

因此,這裏是我的文件夾結構

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

我一定已經嘗試了所有可能的上,下類名,文件名,但錯誤的情況下組合是永遠不變的。


通過一些答案去後,我想可能是我應該在這裏增加一個基本問題 - 它是在所有可能笨內有我自己的自定義對象類型...類型,應該是相當明顯的,從我的問題?


這似乎爲我工作,所以這裏是我會做......至少到一些突破:

在我的控制器的構造函數中,我使用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'); 
    } 
} 
+0

您是否嘗試過使用低級文件名並加載**數組庫**; '$這 - >負載>庫(陣列( 'CGH/cgh_assettype', 'CGH/cgh_asset', 'CGH/cgh_article'));'? –

+0

我已經嘗試過所有可能的組合類名,文件名的大寫和小寫。沒有工作。我試過從類名,文件名中刪除Cgh_前綴。沒有工作。我嘗試將我的類文件移動到應用程序/庫的根目錄中。沒有工作。目前我正在嘗試require_once(APPPATH.'libraries/cgh/Cgh_assettype.php');在index.php – SIndhu

+0

順便說一句,你正在嘗試加載'cgh_assettype',但你的文件被命名爲'cgh_asettype.php',你檢查了嗎? –

回答

1

您需要呼籲$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. (可選)$config陣列
  3. (可選)字符串重命名庫($this->Renamed_library

如果要加載在同一行多個庫,請使用數組作爲第一個參數。

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

感謝火箭。但我嘗試過,努力工作。即使我只加載其中一個類,比如說$ this-> load-> library('cgh/Cgh_assettype'); 它給Cgh_assettype錯誤 – SIndhu

+0

也許CodeIgniter不喜歡'Cgh_'前綴?嘗試刪除。 –

+0

不幸的是,該功能也可以。我嘗試的另一件事是將這些類文件從cgh文件夾移動到庫的根目錄。仍然錯誤仍然... – SIndhu

1

你是庫文件名大寫嗎? (您提交的文件夾結構表明他們不是)。

我不知道在子目錄中有庫,但我知道文件名需要大寫。

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

命名約定

  • 文件名必須大寫。例如:Myclass.php
  • 類聲明必須大寫。例如:class Myclass
  • 類名和文件名必須匹配。
0

您應該只加載一次庫或其他任何東西。如果你第二次加載,你會得到這個錯誤。