2016-02-08 143 views
1

我已經分隔與PHP類的幾個文件,現在我需要讓他們在一起,在一個函數是這樣的:PHP函數調用另一個類

include("require-class.php"); 

require_multi("db-class.php", "settings.php","users-class.php"); 

class ajaxLogin { 
    private $settings;    
    private $users; 

    public function __construct(settings $settings, users $users) { 
    $this->settings = $settings; 
    $this->users = $users; 
    } 
} 

global $ajaxLogin; 
$ajaxLogin = new ajaxLogin; 

settings.php類名爲設置和一個在users-class.php用戶。我得到了一個錯誤:

PHP Catchable fatal error: Argument 1 passed to ajaxLogin::__construct() must be an instance of settings, none given, called in /var/www/html/idcms/admin/class/login-ajax.php on line 47 and defined in /var/www/html/idcms/admin/class/login-ajax.php on line 13, referer: http://localhost/idcms/admin/

+2

你如何調用這個類實例? – AnkiiG

回答

2

你的ajaxLogin構造函數需要兩個參數:$settings$user。所以,你需要撥打:

$ajaxLogin = new ajaxLogin($settings, $user); 

當然,你需要之前實例settingsuser,如:

$settings = new settings(); // add arguments if required 
$user = new user();   // add arguments if required 

UPDATE

如果你真的想要實例settingsuser在你的ajaxLogin裏面,只需從你的__construct這樣刪除參數:

class ajaxLogin { 
    private $settings;   
    private $users; 

    public function __construct() { 
     $this->settings = new settings(); 
     $this->users = new users();   
    } 
} 

然而,第一種方法通常是最好的一個,因爲大家誰使用類ajaxLogin立即知道,這個類取決於settingsuser。有關詳情,請參閱this question

+0

好的,謝謝你,我想知道是否有任何方法可以在函數參數中調用這個函數,謝謝。 –

+0

@PavelKocfelda看到我更新的答案 – simon