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

时间:2012-06-27 15:59:43

标签: php oop

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

这是我目前的代码:

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



?>

如何从数据库类

调用函数

6 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分: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);

答案 2 :(得分: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
...
}

答案 3 :(得分:1)

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

<?php

include 'database.php';


class department
{

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

?>

答案 4 :(得分:1)

创建一个'database'类型的对象,然后使用它调用该方法:

$database = new database();
$connection = $database->connect();

答案 5 :(得分: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);      
   }

}
?>