douba8048
douba8048
2014-04-11 18:03
浏览 39
已采纳

在student类中调用数据库类来执行查询

I have two main classes, Database & Student now I want to execute a query in studentDetail() method of Student Class. i.e.

But I am getting error that "Cannot redclare class Db in database_Class.php line:3"

studnet_Class.php

    include "database_Class.php";

 Class Student{
    private $db,$studentName;

    function __construct($DatabaseObj)
    {
        $this->db=$databaseObj;
    }

    public function studentDetail($ID)
    {
        $this->db->query("student",$ID);
    }
}

index.php

$db= new Db();
$student= new Student($db);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujia4619
    doujia4619 2014-04-11 18:09
    已采纳

    Replace:

    include "database_Class.php";
    

    by

    include_once "database_Class.php";
    

    This makes PHP including the file only if it hasn't included before.

    I would further recommend to use require_once in favour of include_once to import a class. require_once will stop the script immediately throwing a fatal error if the file was not found. As a class file is an essential part of a program, only stopping the program immediately is a sufficient consequence.

    I recommend to read the manual of

    点赞 评论

相关推荐