I’ve seen several examples on the Net using a similar approach but I cannot see why it’s failing here.
I’m trying to use 2 parameters for DB_HOST and DB_DATABASE that I want to append to a string and assign it to the following variable:
External PHP file:
define( 'DB_HOST', 'localhost');
define( 'DB_DATABASE', 'mydb');
class Database {
private static $dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_DATABASE;
...
}
I’ve tried with single quotes, double quotes, and everything I can think of but I’m getting the following error:
Parse error: syntax error, unexpected '.', expecting ',' or ';'
I’ve tried putting the define( ‘DB_HOST’, ‘localhost’); inside the class as well (althogh this won’t be ideal, just for testing purposes) but then I’m getting:
Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION
Any idea what could be going wrong?
PS: I tried as well with normal PHP variables (as in $db_host) without any success either :S
You can’t initialize properties like that, you can only initialize them to a constant value. Even this would give the same error:
You’ll need to initialize it somewhere else. For example: