Instantiating objects in a MVC in PHP 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-&gt;user = new User();
        $this-&gt;db = new Database( DB_TYPE, DB_HOST, DB_NAME, DB_USER, DB_PASS );
    }
}


class User {

    public function getUserId(){        
        return $this-&gt;_userid;  
    }

    public function __construct( ){
        $this-&gt;db = new Database( DB_TYPE, DB_HOST, DB_NAME, DB_USER, DB_PASS );
        $this-&gt;_userid = 1;     
    }
}


Thanks in advance for the help!
In general, using the "new" keyword in any class is bad practice, because you're creating an implicit dependency. Instead, you should follow the "ask, don't look" design philosophy and require that the database object be passed to the class, rather than letting the class fetch it. Dependency injection is a common solve to this problem, and I'd recommend looking at how PIMPLE is used. Here's a good article: "Dependency Injection with Pimple".

Many people, like deceze, will deride you (rightfully so) for doing things like this, because it's one of the easiest (and probably most common) things you can do to kill the possibility of testing your code. If you were to write a unit test for this model, you'd have a database object being constructed with each test. Since the whole point of unit tests is to run code in isolation (to verify the tested code's accuracy), it would be impossible to test this model without a database.

On that note, the JavaScript guru, Misko Hevery, published a really superb article on writing testable code: "Top 10 things which make your code hard to test". While this is specifically in the context of code testing, it's also very good general programming information.