I hava a class called DBhelper, and now I created a new class called UserManager.
I need the UserManager have functions like GetUserId() and CheckValidation().So I require the DBhelper for those database actions. But I found it doesn’t work like this:
require_once('DBhelper.php');
class User{
public $databaseHelper = new DBhelper();//syntax error
public $DB = $databaseHelper->connectDB();//syntax error
public function GetUserId(){
$this->databasesHelper->RunSql();//syntax error
...
}
public function CheckValidation(){
$this->databasesHelper->RunSql();//syntax error
...
}
}
Please halp me, I have searched the internet for long time. thanks.
Try something like this, it uses the dependency injection design pattern.
Here is how you instantiate it:
Also, to touch on your errors, these are errors (note they’re not syntax errors, since they are syntactically correct):
Because you cannot initialize member variables to non-static values. So, since the values are not known at compile time, it is an error.
Similarly, these calls:
Are invalid because
$databasesHelperwas never instantiated with a proper object.