2013-03-14 33 views
0

我想在我的codeigniter控制器中添加一個ucfirst()函數,所以我會找回第一個大寫字母的字符串。在...第7行(在我的ucfirst是行)在php 5.3中使用ucfirst()函數返回一個解析錯誤

;語法錯誤,意外「(」,希望「」或‘’:出於某種原因,我不斷收到一個解析錯誤:

解析錯誤。

試圖改變ucfirst()來ucfirst(用strtolower($數據庫))或ucwords($數據庫)返回相同的結果

我的代碼是:

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

class Somecontroller extends CI_Controller { 
    private $database = "some_database"; 
    private $model = ucfirst($this->database)."_model"; 
    ... 
} 

回答

4

php doc

declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

你應該初始化$model財產類的構造函數

public function __construct() 
{ 
    // I guess you'll need to call parent constructor as well 
    parent::__construct(); 
    $this->model = ucfirst($this->database) . '_model'; 
} 
+0

謝謝!如你所說,問題已解決。 – 2013-03-14 18:14:25

0

您應該使用這 - $>數據庫,並在構造函數分配

喜歡的東西:

function __construct() 
{ 
    $this->model = ucfirst($this->database) . "whatEver"; 
} 
+0

你是對的!我修正了我的錯誤,但我仍然得到了解析錯誤... – 2013-03-14 14:02:40

+0

看到我的更改...使用構造函數... – 2013-03-14 14:03:16

+0

將它移動到我的約束器,但仍然出現parase錯誤:解析錯誤:語法錯誤,意外的T_PRIVATE – 2013-03-14 14:10:06