Hi all,
I'm trying to learn how classes work, they seem useful 🙂
I am trying to create a class to handle all my database actions, so I can handle all database stuff with simple commands like $DB->connect(); and such.
I have create a class called DB and am trying to set some constants (the SQL info). The first function I am creating is just a test function to see if the connection to the database is made properly, however it won't parse. This is my code:
// Database class
class DB {
// Get database info and set as constants
const sqlhost = 'localhost';
const sqluser = 'root';
const sqlpass = 'topsecret';
const sqldb = 'testdb';
function connecttest() {
$sql = mysql_connect($sqlhost,$sqluser,$sqlpass) or die('Unable to connect to MySQL server.');
mysql_select_db($sqldb) or die('Unable to select database.');
$this->result = echo "Connection Succesful !";
mysql_close($sql);
return $this->result;
}
}
I get this parsing error:
parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}'
Why is the const unexpected ? I get the same error when I copy/paste exactly this sample code from php.net:
class MyClass
{
const constant = 'constant value';
function showConstant() {
echo self::constant . "\n";
}
}
echo MyClass::constant . "\n";
$class = new MyClass();
$class->showConstant();
Is it something with my server configuration ? I really don't understand.