gpt4 book ai didi

php - 在 php 中,如何从另一个类中的另一个类调用函数?

转载 作者:行者123 更新时间:2023-12-03 18:33:17 27 4
gpt4 key购买 nike

如何从另一个类中的另一个类调用函数?

这是我当前的代码:

    <?php

class database
{
private $host = "localhost";
private $user = "root";
private $password = "";
private $db = "9dot";

//connect to database
public function connect()
{
$connect = mysql_connect($this->host,$this->user,$this->password);
$database = mysql_select_db($this->db,$connect);

}



//Get exam headers
public function GetExam()
{
$this->connect();
$select = "SELECT e.ExamHeaderID, CONCAT(e.Firstname,' ',e.Lastname) AS Fullname e.Age, e.Position, e.Date FROM examheader e";
$result = mysql_query($select);
$table = "";
$table .= "<th>ExamID</th>";
$table .= "<th>Name</th>";
$table .= "<th>Age</th>";
$table .= "<th>Position</th>";
$table .= "<th>Date</th>";

$exam_id = "";
$fullname = "";
$age = "";
$position = "";
$date = "";
while($row = mysql_fetch_array($select))
{
$exam_id = mysql_real_escape_string($row['ExamHeaderID']);
$fullname = mysql_real_escape_string($row['Fullname']);
$age = mysql_real_escape_string($row['Age']);
$position = mysql_real_escape_string($row['Position']);
$date = mysql_real_escape_string($row['Date']);
}

}

public function GetExamDetails($ExamID)
{
$this->connect();
//$select = "SELECT ed.ExamDetailsID, ed.";
}


//Save Exam header
public function SaveExam($firstname,$lastname, $age, $position)
{
$this->connect();
$date = date("m/d/Y");
mysql_real_escape_string($firstname);
mysql_real_escape_string($lastname);
mysql_real_escape_string($age);
mysql_real_escape_string($position);
$insert = "INSERT INTO examheader (Firstname,Lastname, Age, Position, Date) VALUES ('$firstname','$lastname','$age','$position')";
$result = mysql_query($insert);
$exam_id = mysql_insert_id();
//return exam id for insertion of exam details
return $exam_id;

}

public function SaveExamAnswer()
{


}



}

?>





<?php

include 'database.php';
$database = new $database();

class department
{
$database->//property or function
}



?>

如何从数据库类调用函数

最佳答案

您可以在部门类中声明数据库的实例,如下所示:

class department
{
public function __construct()
{
$database = new database();
$database->someFunction();
}
}

如果你不想直接在你的部门类中使用它,你可以使用 global 声明它。系统,假设包含所需的文件,如下所示:
include 'database.php'

$database = new database();

class department
{
public function __construct()
{
global $database;
$database->someFunction();
}
}

PHP: Variable scope

关于php - 在 php 中,如何从另一个类中的另一个类调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11230441/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com