RuleBritannia Posted May 13, 2014 Share Posted May 13, 2014 (edited) Hello I hear alot of talk about reusing a database connection and attempts to solve this. I came across this post on stackoverflow but am having difficulty understanding as to how this is any different than just creating a instance in a single file and including it via include, require or autoload wherever a db instance is needed. The pattern talked about in the post is the factory pattern, it seems that the factory is responsible for creating the instance, something which I thought autoloading did without this additional code? Another thing I am still confused by with all the reuse talk - If 2 seperate users request 2 different pages on our server(2nd user 0.1 second behind the other user) which both refer back to the same db file/factory, are 2 instances created? or is the previuos users instance somehow used aslong as there request has not finished? I thought that the server would make new fresh requests/variables/instantiation etc all over again for every user, perhaps I am wrong. Thanks in advance. Edited May 13, 2014 by RuleBritannia Quote Link to comment https://forums.phpfreaks.com/topic/288469-factory-pattern-and-autoloading/ Share on other sites More sharing options...
Jacques1 Posted May 13, 2014 Share Posted May 13, 2014 Hi, you're confusing many different things which have nothing to do with each other. Autoloading automatically includes class definitions, nothing more, nothing less. It does not create instances in any way. The Factory Pattern is a specific way of instantiating classes. It's meant to make the instantiation more flexible and allow you to easily exchange one class for a similar class. The term “reuse” refers to code reuse. The point is that you are able to reuse your code in different contexts instead of starting all over again everytime. It has nothing to do with resource sharing or anything like that. Quote Link to comment https://forums.phpfreaks.com/topic/288469-factory-pattern-and-autoloading/#findComment-1479393 Share on other sites More sharing options...
RuleBritannia Posted May 13, 2014 Author Share Posted May 13, 2014 (edited) Hi, you're confusing many different things which have nothing to do with each other. Autoloading automatically includes class definitions, nothing more, nothing less. It does not create instances in any way. The Factory Pattern is a specific way of instantiating classes. It's meant to make the instantiation more flexible and allow you to easily exchange one class for a similar class. The term “reuse” refers to code reuse. The point is that you are able to reuse your code in different contexts instead of starting all over again everytime. It has nothing to do with resource sharing or anything like that. I should of been more specific, If I instantiate a class(lets say connection) and store it in a variable, then include this everywhere needed and pass around in constructors, why would I need a factory? As for the term reuse meaning in a code reuse context, I dont believe that is how it was meant, I am sure I have seen on many occasions people saying "you dont want to be creating a new connection, you reuse a database connection already opened." The problem I have here is understanding how if we use factory or autoload, each one instansiate's lets say a connection(with new), how is this reusing a connection? Edited May 13, 2014 by RuleBritannia Quote Link to comment https://forums.phpfreaks.com/topic/288469-factory-pattern-and-autoloading/#findComment-1479394 Share on other sites More sharing options...
Jacques1 Posted May 13, 2014 Share Posted May 13, 2014 You should look up how autoloading actually works and what it does. I think this will eliminate the confusion. You cannot “instantiate a class and autoload it everywhere”. As I already said, autoloading can be used to load class definitions. You cannot load arbitrary scripts. I think you should forget about the Factory Pattern for now. It has absolutely nothing to do with the general problem of instantiating classes. It's an advanced design concept which you may use when you're familiar with the basics and try to improve your code. Resource sharing is yet another topic. I think you should postpone this as well. Before you jump into the advanced stuff, make sure you understand the basics (like autoloading). Quote Link to comment https://forums.phpfreaks.com/topic/288469-factory-pattern-and-autoloading/#findComment-1479395 Share on other sites More sharing options...
RuleBritannia Posted May 13, 2014 Author Share Posted May 13, 2014 You should look up how autoloading actually works and what it does. I think this will eliminate the confusion. You cannot “instantiate a class and autoload it everywhere”. As I already said, autoloading can be used to load class definitions. You cannot load arbitrary scripts. I think you should forget about the Factory Pattern for now. It has absolutely nothing to do with the general problem of instantiating classes. It's an advanced design concept which you may use when you're familiar with the basics and try to improve your code. Resource sharing is yet another topic. I think you should postpone this as well. Before you jump into the advanced stuff, make sure you understand the basics (like autoloading). So using the factory pattern, does this allow resource sharing? As for autoloading and including I should not of mixed them throughout this thread, but what I was trying to say is if I had a file with the following code db.php $db = new PDO('... If I include this everywhere a db instance is needed, how is this different to calling a factory pattern class(Which creates or reuses a instance)? Arnt either of these methods using a new created instance at the begining and resuing a instance if called anytime after? Quote Link to comment https://forums.phpfreaks.com/topic/288469-factory-pattern-and-autoloading/#findComment-1479396 Share on other sites More sharing options...
trq Posted May 13, 2014 Share Posted May 13, 2014 So using the factory pattern, does this allow resource sharing? No. As for autoloading and including I should not of mixed them throughout this thread Indeed, autoloading is not what you think it is. If I include this everywhere a db instance is needed, how is this different to calling a factory pattern class(Which creates or reuses a instance)? You still don't understand what a factory is for. On a side note, creating some $db variable and passing it around everywhere is going to be unmaintainable and has a very high potential of breaking. Don't rely on global state. Quote Link to comment https://forums.phpfreaks.com/topic/288469-factory-pattern-and-autoloading/#findComment-1479398 Share on other sites More sharing options...
Jacques1 Posted May 13, 2014 Share Posted May 13, 2014 Creating an instance and including it in different scripts also gives you only a single object, whereas autoloading gives you the class and lets you instantiate it as often as you want. Those are obviously two entirely different things. For example, let's say I have a File class which represents one file, and I want to use this class everywhere in my project. It wouldn't help me to create a single instance and pass it around. But auto-loading the class does make sense. Last but not least, I think you confuse simple static factory methods with The Factory Pattern. Those are two different things. Quote Link to comment https://forums.phpfreaks.com/topic/288469-factory-pattern-and-autoloading/#findComment-1479400 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.