doujiao9574 2013-03-14 05:58
浏览 27
已采纳

在php 5.3中使用ucfirst()函数返回一个解析错误[关闭]

I'm trying to add a ucfirst() function in my codeigniter controller so I'll get back a string with a first upper case letter. For some reason I keep getting a parse error:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in ... on line 7 (the line where my ucfirst is).

Trying to change ucfirst() to ucfirst(strtolower($database)) or to ucwords($database) returning the same result.

My code is:

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

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

2条回答 默认 最新

  • dscdttg4389 2013-03-14 05:59
    关注

    From 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.

    You should initialise $model property in class constructor

    public function __construct()
    {
       // I guess you'll need to call parent constructor as well
       parent::__construct();
       $this->model = ucfirst($this->database) . '_model';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?