I am working on converting a PHP application to MVC, and have a couple questions!
1) I have a main Model object, that I require a database connection for, as well as a User object that uses a database object also. Is it a bad practice to create a new database object for each of these classes, like the following?
or should I create a $databaseObject variable on the index.php page and pass that same variable in as a construct parameter for both User and Model? What is the difference?
Is there anything else that sticks out as something that I am doing wrong?
class Model {
function __construct() {
$this->user = new User();
$this->db = new Database( DB_TYPE, DB_HOST, DB_NAME, DB_USER, DB_PASS );
}
}
class User {
public function getUserId(){
return $this->_userid;
}
public function __construct( ){
$this->db = new Database( DB_TYPE, DB_HOST, DB_NAME, DB_USER, DB_PASS );
$this->_userid = 1;
}
}
Thanks in advance for the help!