1

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?

1
  • 1
    If you use the same object, one connection. Commented Nov 19, 2012 at 3:35

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.