PHP function to call another classes -
i have seperated few file php classes, , need them in 1 function 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
named settings , 1 in users-class.php
users. got error:
php catchable fatal error: argument 1 passed ajaxlogin::__construct() must instance of settings, none given, called in /var/www/html/idcms/admin/class/login-ajax.php on line 47 , defined in /var/www/html/idcms/admin/class/login-ajax.php on line 13, referer: http://localhost/idcms/admin/
your constructor of ajaxlogin
requires 2 arguments: $settings
, $user
. need call:
$ajaxlogin = new ajaxlogin($settings, $user);
of course need instantiate settings
, user
before, like:
$settings = new settings(); // add arguments if required $user = new user(); // add arguments if required
update
if want instantiate settings
, user
inside ajaxlogin
, remove arguments __construct
this:
class ajaxlogin { private $settings; private $users; public function __construct() { $this->settings = new settings(); $this->users = new users(); } }
however, first approach better 1 because uses class ajaxlogin
knows class depends on settings
, user
. see this question more detailed answers this.
Comments
Post a Comment