jonniejoejonson Posted September 2, 2009 Share Posted September 2, 2009 I have seen lots of classes that are used to connect to a databases.. however why would you do this?.. surely it is easier to simply use one include statement at the top of your page that connects to the database... and one at the bottom that closes the connection... am i completely missing the point but unless you are using several databases to use a class is pointless? Quote Link to comment https://forums.phpfreaks.com/topic/172843-when-to-use-classes/ Share on other sites More sharing options...
trq Posted September 2, 2009 Share Posted September 2, 2009 There are allot of database classes around that do nothing useful. there is no point in using or creating a class unless it adds value / functionality to your code. Having said that, there are also plenty of database classes around that do add functionality. Theres even a nice data access object (pdo) built right into php these days. Quote Link to comment https://forums.phpfreaks.com/topic/172843-when-to-use-classes/#findComment-910950 Share on other sites More sharing options...
JonnoTheDev Posted September 2, 2009 Share Posted September 2, 2009 there is no point in using or creating a class unless it adds value / functionality to your code. And is not tightly coupled to a single application making it completely reusable . Quote Link to comment https://forums.phpfreaks.com/topic/172843-when-to-use-classes/#findComment-911012 Share on other sites More sharing options...
Garethp Posted September 2, 2009 Share Posted September 2, 2009 I'd say use classes when they do make your code shorter when you're going to be using that code over and over and over... and over. But I'd say as a personal rule, I'd only use classes if I can make the class flexible, and I'll use it atleast 3 times within the next month, and it saves atleast 7 lines of code. Or when I'm so bored I want to write a class because I can Quote Link to comment https://forums.phpfreaks.com/topic/172843-when-to-use-classes/#findComment-911018 Share on other sites More sharing options...
mikesta707 Posted September 2, 2009 Share Posted September 2, 2009 As far as databases go, I generally just have some functions that I use all the time, but I usually don't bother making a class because you get the same functionality when you don't use a class pretty much. I COULD add some extra functionality if I wanted to, but all in all it's not worth the effort in my opinion. However, classes do come in handy when you have a lot of code that is reused a lot, and it would benefit from OOP functionality like encapsulation, overriding functions, inheritance, etc. Like, say you have a general class that you can use with a bunch of different types of data. its so general that it will always (or almost always) work with all your input, but you can also override certain functions, or even add news ones to customize it to your needs. Quote Link to comment https://forums.phpfreaks.com/topic/172843-when-to-use-classes/#findComment-911026 Share on other sites More sharing options...
jonniejoejonson Posted September 3, 2009 Author Share Posted September 3, 2009 Thanks for the responses, however i have found that it is surely easier to use include statemnts... for examlpe i can have an include statment that does a login check etc.. As i can see the only thing that i can't do with include statments is that the object is not iunique like with OOP... however when ni a practical sense is that aspect ever needed? thanks again to all you responders. Quote Link to comment https://forums.phpfreaks.com/topic/172843-when-to-use-classes/#findComment-911717 Share on other sites More sharing options...
KevinM1 Posted September 3, 2009 Share Posted September 3, 2009 Thanks for the responses, however i have found that it is surely easier to use include statemnts... for examlpe i can have an include statment that does a login check etc.. As i can see the only thing that i can't do with include statments is that the object is not iunique like with OOP... however when ni a practical sense is that aspect ever needed? thanks again to all you responders. Or, you could use __autoload(). Quote Link to comment https://forums.phpfreaks.com/topic/172843-when-to-use-classes/#findComment-911738 Share on other sites More sharing options...
JonnoTheDev Posted September 3, 2009 Share Posted September 3, 2009 Thanks for the responses, however i have found that it is surely easier to use include statemnts By the sound of things the projects you work on suit a procedural approach. This is fine and usually the easiest and quickest approach to take. However, if you ever get to work on or design much larger systems that require major additions / modifications over time then you will see in the long run that an OOP approach is by far the most suited. At a first look at OOP concepts it is hard to picture how they benefit over your usual procedural take, however once you become accustomed to it you will not want to revert back to your current methods. Think of an application like Facebook written in PHP. To add to the functionality or modify part of the system you don't think that they would be scouring through hundreds of script files to modify includes or add functions, the core components of the system will be extended using an OOP approach so none of the main source code is affected. Quote Link to comment https://forums.phpfreaks.com/topic/172843-when-to-use-classes/#findComment-911775 Share on other sites More sharing options...
jonniejoejonson Posted September 3, 2009 Author Share Posted September 3, 2009 Thanks Neil for the response, but im still confused...i have written a social network similar to that of facebook.. perhaps a little smaller in size, and a few less than 100 million active users!... however i have done it procedurlly using include statemnts and i can't see which parts of the site i would have used classes for?... do facebook use classes for the login section?... for displaying peoples images?... where and when is it used?.. and why is it advantageous to have written it OOP?.. Surely if you want to make changes to the site's design and functionality is going to be just as hard in OOP? isn't OOP infact slower becuase half the time you are calling things that aren't needed? thanks for your contiunued response... (im just looking for one practical example of OOP) not a tutorial where i have to imagine a dog is an object... a real life example of an aplication that uses it and why they use it. Quote Link to comment https://forums.phpfreaks.com/topic/172843-when-to-use-classes/#findComment-911791 Share on other sites More sharing options...
mikesta707 Posted September 3, 2009 Share Posted September 3, 2009 If you have ever used to Facebook API, you would be able to see why you would never want to use anything but a class for that (at least the PHP API) If you want to see OOP in action, make a facebook APP. You won't really be doing OOP, but you will be using a class, and once you start using a class you begin to see why in certain instances classes are better, and why, if the folks at Facebook had took a procedural, rather an an OO approach, the API would not have been nearly as powerful, and wouldn't be nearly as easy to add functionality to it BTW you can include class files. In fact, including files doesn't really have anything to do with OOP or Procedural programming, its just using a group of functions/variables from a different file. In the case of OOP however, those functions/variables are all wrapped inside a class. When you want to make changes to a site, or add functionality, usually it is much easier with classes. For example, say I have a class I use in a bunch of different scripts. For example, I built a class that outputs data from a query, and it allows you to filter through the data, sort it, etc. If I used a procedural approach, not only would I be repeating a crap load of different code on every page, but if I wanted to add certain functionality to every page, I would have to edit every page seperately. With an OOP approach, I could just edit my class, and now every page has that functionality. Now say I wanted certain pages to have said functionality, but other pages have a slightly different functionality. Again, with an OOP approach, this would be alot easier. Quote Link to comment https://forums.phpfreaks.com/topic/172843-when-to-use-classes/#findComment-911813 Share on other sites More sharing options...
JonnoTheDev Posted September 3, 2009 Share Posted September 3, 2009 do facebook use classes for the login section?... for displaying peoples images You are thinking very horizontally. OOP is not about using a class or classes for bits of functionality. you have seen objects used to connect to databases, etc. This is not OOP, they are just basically wrappers for bits of code that perform general functions and this is why you are not seeing the benefits. OOP is where objects interact with each other in an application. Classes model real life objects. i.e Teacher, Pupil, Classroom A teacher belongs in a classroom, a pupil is allocated many teachers, a pupil has many classrooms, etc. This is OOP interaction. You really need to study from a book. Good example: http://www.phparch.com/books/isbn/0973589825 Also look at a framework like zend. maybe look at how the mvc pattern works to see OOP in action. Quote Link to comment https://forums.phpfreaks.com/topic/172843-when-to-use-classes/#findComment-912061 Share on other sites More sharing options...
jonniejoejonson Posted September 4, 2009 Author Share Posted September 4, 2009 Thanks again... i will take a look at your suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/172843-when-to-use-classes/#findComment-912424 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.