5

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

这是我当前的代码:

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



?>

如何从数据库类调用函数

4

6 回答 6

8

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

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:变量范围

于 2012-06-27T16:07:37.890 回答
3

您可以扩展它们。

class department extends database {
    function test() {
        $this->getdbfunction();
    }
}

或者你把它们交给构造函数:

class department {
    private $db;

    public function __construct(Database db) {
        $this->db = $db
    }
}

$department = new department($database);
于 2012-06-27T16:03:38.987 回答
2

您可以定义一个属性,它是第一个类的实例,然后将方法应用于第二个类示例中定义的属性:

class A {
function a () {
}
...
}

class B {
$attClassA;
function __construct(A objA) {
$this->attClassA = objA;
}
// you can use the attClassA in any class you want and then apply the chosen method
...
}
于 2012-06-27T16:05:35.577 回答
1

尝试这样的事情(粗略的例子):

<?php

include 'database.php';


class department
{

    public function doSomething() {
       $database = new $database();
       $val = $database->GetExam();
       //do something
    }
}

?>
于 2012-06-27T16:01:53.317 回答
1

创建一个“数据库”类型的对象,然后使用它调用该方法:

$database = new database();
$connection = $database->connect();
于 2012-06-27T16:03:47.060 回答
0

你可以这样做:

如果您不想手动包含 PHP 文件,请使用 spl_autoload_register() 函数,否则您需要包含具有要从中导入方法的类的 PHP 文件。

file-A.php - 包含 A 类的 PHP 文件

<?php

class A{

   function DoSquare($x){
      return $x*$x;
    }

}

?>

file-B.php - 包含 B 类的 PHP 文件

假设您正在使用 spl_autoload_register():

<?php

class B{
   
   private $ObjectA = NULL;

   public function __construct(){
        $this->ObjectA = new A();
    }

   public function PrintSquare($x){
       echo $this->ObjectA->DoSquare($x);      
   }

}
?>

如果手动包含 PHP 文件:

<?php

require("file-A.php");

class B{
   
   private $ObjectA = NULL;

   public function __construct(){
        $this->ObjectA = new A();
    }

   public function PrintSquare($x){
       echo $this->ObjectA->DoSquare($x);      
   }

}
?>
于 2020-08-28T12:56:45.550 回答