构造PHP不工作显示空白在我的浏览器


construct php not work show blank in my browser

我创建了一个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();