我创建了一个PHP类文件,但是当它运行时它不工作
class User {
public function __construct(){
$host_name='localhost';
$username='root';
$password='';
$database_name='php';
$conn=mysql_connect($host_name,$username,$password);
if(!$conn)
{
die('Database Not Connected!');
}
else{
echo 'Database connected';
}
}
}
需要使用new
关键字实例化类。记住要使用圆括号来调用类构造函数方法:
class User {
public function __construct(){
$host_name='localhost';
$username='root';
$password='';
$database_name='php';
$conn=mysql_connect($host_name,$username,$password);
if(!$conn)
{
die('Database Not Connected!');
}
else{
echo 'Database connected';
}
}
}
$myUser = new User();