phpcodeigniterparsingucfirst

Using ucfirst() function in php 5.3 returning a parse error


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";
    ...
}

Solution

  • 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';
    }