简体   繁体   中英

How to call function in another class

how to call to a function in another class. I can't explain, but i hope you will understand my problem.

Notice: Undefined variable: db in class general.

http://pastebin.com/C1aY1US5

Inject it:

class general{
    public function __construct(db $db){
        $this->db = $db;
    }
    public function userId($nickname) {
        $result = $this->db->query('SELECT `id` FROM `users` WHERE `username` = $nickname'); //there i want to call to db class

        return($this->db->fetch($result));
    }
}

And later:

$db = new db();
$general = new general($db);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM