如何在另一个类中使用函数

时间:2015-03-28 14:21:29

标签: php oop

我是OOP中的新手。我有文件database.php

class Database{
    function db_row($con,$stuff,$table,$statements){
        return mysqli_fetch_array(mysqli_query($con,"SELECT {$stuff} FROM `{$table}` {$statements}"));
    }}

我有文件player.php

class Player{
    function get_id($con,$token){
    }}

我想在类Player(文件player.php)中使用函数db_row-class Database(file database.php)

我该怎么做?

1 个答案:

答案 0 :(得分:1)

你可以尝试下面这样的事情。基本上,您在Database的构造函数中实例化Player对象,然后您可以访问DatabasePlayer内的方法,如下所示。

<?php

    class Database{
        function db_row($con,$stuff,$table,$statements){
            echo "Success";
            //return mysqli_fetch_array(mysqli_query($con,"SELECT {$stuff} FROM `{$table}` {$statements}"));
    }}

    class Player {
        var $db;

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

        function get_id($con,$token){
            $this->db->db_row(null, null, null, null);
        }
    }

    $player = new Player();
    $player->get_id(null, null);

?>