1

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/

1
  • 2
    How you have called this class instance? Commented Feb 8, 2016 at 8:37

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, thank you i wanted to know if there is any way to call this inside the function arguments, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.