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 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序