If I have a database class, and pass an instance of that class to the constructors of other classes, will it still only be using one connection or will it create as many connections as constructors I pass it to?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Generally speaking, an object that gets passed to a function or is assigned to a variable (without using
clone) will increase the reference count but will not create another instance.If an object is cloned, all properties are copied into a new object; scalar properties will get duplicated, other properties (objects, resources) will instead have an increased reference count.
Conclusion
Assuming you have created a single instance of your database class and the database connection is created once inside the constructor and stored inside a property, passing it around will not cause multiple connections to be opened.