Home Ask Login Register

Developers Planet

Your answer is one click away!

Pavel Kocfelda February 2016

PHP function to call another classes

I have seperated a few file with PHP classes, and now I need to get them together in one function like this:


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;

Class in settings.php is named settings and the one in users-class.php is users. I got an error:

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/


simon February 2016

Your constructor of ajaxLogin requires two arguments: $settings and $user. So you need to call:

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

Of course you need to instantiate settings and user before, like:

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


If you really want to instantiate settings and user inside your ajaxLogin, just remove the arguments from your __construct like this:

class ajaxLogin {
    private $settings;          
    private $users;

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

However, the first approach is usually the better one because everyone who uses the class ajaxLogin knows immediately that this class depends on settings and user. See this question for more detailed answers about this.

Post Status

Asked in February 2016
Viewed 3,022 times
Voted 5
Answered 1 times


Leave an answer

Quote of the day: live life