koca79331
koca79331

Reputation: 421

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:

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;

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/

Upvotes: 1

Views: 75

Answers (1)

simon
simon

Reputation: 2946

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

UPDATE

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.

Upvotes: 2

Related Questions