Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 调用另一个类中的类_Php - Fatal编程技术网

Php 调用另一个类中的类

Php 调用另一个类中的类,php,Php,您好,我在调用另一个类中的类时遇到一些问题,下面的代码1可以显示结果,但在2中使用相同的代码添加到另一个类中,我不知道为什么它不起作用。 谢谢 一, 需要“database.php”; $database=新数据库(); $database->query('SELECT user\u id,user\u email FROM tb其中user\u id=:user\u id'); $database->bind(':user_id',1'); $rows=$database->resultset

您好,我在调用另一个类中的类时遇到一些问题,下面的代码1可以显示结果,但在2中使用相同的代码添加到另一个类中,我不知道为什么它不起作用。
谢谢

一,

需要“database.php”;
$database=新数据库();
$database->query('SELECT user\u id,user\u email FROM tb其中user\u id=:user\u id');
$database->bind(':user_id',1');
$rows=$database->resultset();//费考尔
回声“;
打印(行);
回声“;
二,

需要“database.php”;
$database=新数据库();
课堂测试{
公共函数testf(){
打印“日志”;
$database->query('SELECT user\u id,user\u email FROM tb其中user\u id=:user\u id');
$database->bind(':user_id',1');
$rows=$database->resultset();//fetchall
回声“;
打印(行);
回声“;
打印“日志”;
}
}
$foo=新测试();
$foo->testf();

请注意,
$database
仅在全局范围内可用,而不在
测试的范围内。将其更改为:

    require 'database.php';


    class test{
var $database;
public function test(){
$database = new Database();
}
        public function testf(){
            print"log";
            $this->database->query('SELECT user_id, user_email FROM tb WHERE user_id = :user_id');
            $this->database->bind(':user_id', '1');
            $rows = $database->resultset(); // fetchall

            echo "<pre>";
            print_r($rows);
            echo "</pre>";
            print"log";
        }
    }
    $foo = new test();
    $foo -> testf();
需要“database.php”;
$database=新数据库();
课堂测试{
公共函数testf($database){
打印“日志”;
$database->query('SELECT user\u id,user\u email FROM tb其中user\u id=:user\u id');
$database->bind(':user_id',1');
$rows=$database->resultset();//fetchall
回声“;
打印(行);
回声“;
打印“日志”;
}
}
$foo=新测试();
$foo->testf($database);

另一种选择是将$database作为类变量(听起来更好)。然后这样做:

类测试{
受保护的数据库;
公共函数构造(){
$this->database=新数据库();
}
公共函数testf(){
打印“日志”;
$this->database->query('SELECT user\u id,user\u email FROM tb其中user\u id=:user\u id');
$this->database->bind(':user_id',1');
$rows=$this->database->resultset();//fetchall
回声“;
打印(行);
回声“;
打印“日志”;
}   
}
$foo=新测试();
$foo->testf();
试试这个:

需要“database.php”;
课堂测试{
var$数据库;
公共功能测试(){
$database=新数据库();
}
公共函数testf(){
打印“日志”;
$this->database->query('SELECT user\u id,user\u email FROM tb其中user\u id=:user\u id');
$this->database->bind(':user_id',1');
$rows=$database->resultset();//fetchall
回声“;
打印(行);
回声“;
打印“日志”;
}
}
$foo=新测试();
$foo->testf();

为什么不起作用?你希望这能实现什么?你试过什么?您遇到了哪些错误?请阅读:
require 'database.php';
$database = new Database();

class test{
    public function testf(){
        print"log";
        $database->query('SELECT user_id, user_email FROM tb WHERE user_id = :user_id');
        $database->bind(':user_id', '1');
        $rows = $database->resultset(); // fetchall

        echo "<pre>";
        print_r($rows);
        echo "</pre>";
        print"log";
    }
}
$foo = new test();
$foo -> testf();
require 'database.php';
$database = new Database();

class test{
    public function testf($database){
        print"log";
        $database->query('SELECT user_id, user_email FROM tb WHERE user_id = :user_id');
        $database->bind(':user_id', '1');
        $rows = $database->resultset(); // fetchall

        echo "<pre>";
        print_r($rows);
        echo "</pre>";
        print"log";
    }
}

$foo = new test();
$foo -> testf($database);
class test {

    protected $database; 

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


    public function testf(){
        print"log";
        $this->database->query('SELECT user_id, user_email FROM tb WHERE user_id = :user_id');
        $this->database->bind(':user_id', '1');
        $rows = $this->database->resultset(); // fetchall

        echo "<pre>";
        print_r($rows);
        echo "</pre>";
        print"log";
    }   

}

$foo = new test();
$foo -> testf();
    require 'database.php';


    class test{
var $database;
public function test(){
$database = new Database();
}
        public function testf(){
            print"log";
            $this->database->query('SELECT user_id, user_email FROM tb WHERE user_id = :user_id');
            $this->database->bind(':user_id', '1');
            $rows = $database->resultset(); // fetchall

            echo "<pre>";
            print_r($rows);
            echo "</pre>";
            print"log";
        }
    }
    $foo = new test();
    $foo -> testf();