【问题标题】:How to call function in another class如何在另一个类中调用函数
【发布时间】:2015-10-30 11:58:15
【问题描述】:

如何调用另一个类中的函数。我无法解释,但我希望你能理解我的问题。

注意:未定义变量:一般类中的 db。

http://pastebin.com/C1aY1US5

【问题讨论】:

    标签: php class mysqli


    【解决方案1】:

    注入它:

    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));
        }
    }
    

    后来:

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

    【讨论】:

    • 谢谢! :) 很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多