Assuming I have a class like this:
class Database
{
public static $hostname = 'localhost';
}
Is there any way to do this?
$createDatabaseQuery = "CREATE DATABASE IF NOT EXISTS Database::$hostname";
Currently I’m getting an error when trying to access the class from within the string and I’d like to avoid this:
$hostname = Database::$hostname;
$createDatabaseQuery = "CREATE DATABASE IF NOT EXISTS $hostname";
In double quotes you can only use “variable expressions”. They must always start with a
$or{$to be interpreted. And you cannot access a class variable directly without a class qualifier.The only workaround offerable is this:
But that’s only a different kind of worse.
And this would be the “variable expression” alternative: