Page 1 of 1

Overuse of classes ?

Posted: Thu Oct 28, 2010 2:06 pm
by motion2000
My friend and i are working on a project together,, he has a lot more programming experience then i do, however as he was leading the way he has created some very hard to read and understand code. My main question is, when you are working with PHP and MYSQL, does it make sense to create classes that represent your database tables? My friend tells me there is no other way to do it with out classes, but to me it seems there almost has to be a better way to do it. He says its way easier to make classes and put functions in that class files instead of making one big function file. For some reason i don't see the purpose of using classes the way that we are using them.. What were currently doing is making a class with the same name as the table, and then making the fields, objects with in the class.. Is this a good way to be doing things ? I'm sorry if this is in the wrong part of the forum to be posting this.. Its not a direct code question, as much as a concept question so i didn't know for sure which area to post in.. Thanks in advance for any help !

Re: Overuse of classes ?

Posted: Thu Oct 28, 2010 3:06 pm
by pickle
I don't usually do this, but this practice is usually called ORM [ Wikipedia ]. It's not true that ORM is the only way to interact with database tables, but it is a commonly used one.

Re: Overuse of classes ?

Posted: Thu Oct 28, 2010 3:43 pm
by Eran
This method is actually the Table Data Gateway pattern. ORM usually abstract on the row level - mapping each entry to an object, while data gateways abstract on the table level.

This is a very useful pattern, once you get used to it. It's easy to tell where to look for logic that handles a specific table - just look in its Gateway class. On the other hand, a large file with many functions is much harder to maintain as the project grows - very soon you have difficult locating specific functions and organizing data efficiently. Those functions also cannot share information easily - they must pass it around or use global variables, while a data gateway class can store all the information it needs to access and manipulate the table.

Re: Overuse of classes ?

Posted: Thu Oct 28, 2010 4:26 pm
by Jonah Bron
The pattern you are used to is called Transaction Script. One (probably procedural) script handles each request on it's own. What you are encountering in larger scale is called Object Oriented Programming (OOP). A great way to learn about OOP is to write some programs in very a very object oriented language. Examples are Java, Ruby, Javscript. Javascript is particularly cool in my opinion.

It is my understanding that PHP started as a procedural language, and objects were added in version 3 and 4. So, they aren't quite as an integral part of PHP as other languages that were created with that in mind. But don't misunderstand me; PHP is very powerful, and is just as good at handling objects as most other languages out there.

Re: Overuse of classes ?

Posted: Thu Oct 28, 2010 4:38 pm
by Benjamin
LOL You guys called it 3 different things. I think at the end of the day he is just creating models for each table, which is pretty much what I do.

Indeed your friend is right. You can learn a lot from him.

Re: Overuse of classes ?

Posted: Thu Oct 28, 2010 6:40 pm
by motion2000
Thank you all for your reply's, that is all i needed to know.. Now that i wont be so busy doubting it i should be able to learn and understand it better.. Didn't want to pick up any bad habits~!

Re: Overuse of classes ?

Posted: Thu Oct 28, 2010 7:19 pm
by Jonah Bron
Jonah Bron wrote:What you are encountering in larger scale is called Object Oriented Programming (OOP).
:drunk:

Re: Overuse of classes ?

Posted: Thu Oct 28, 2010 7:45 pm
by Benjamin
Jonah Bron wrote:
Jonah Bron wrote:What you are encountering in larger scale is called Object Oriented Programming (OOP).
:drunk:
MVC

:drunk:

Re: Overuse of classes ?

Posted: Fri Oct 29, 2010 8:17 am
by alex.barylski
LOL You guys called it 3 different things. I think at the end of the day he is just creating models for each table
Thats what I thought at first read, but if you look more closely, pickle and eran are speaking of the current project and jonah seems to be refering to past projects the OP probably worked on, transaction scripts are typically what new developer start with as it lessens the learning curve, which I think OP implied.

I agree that it sounds more like a TDG but ORM is probably the most vague pattern (or at least misunderstood) of all data patterns, so you could possibly call it an ORM as well, especially considering none of us have any idea exactly what it does. It might be a TDG at the interface but implementing all sorts of ORM responsibility. :)

Cheers,
Alex

Re: Overuse of classes ?

Posted: Fri Oct 29, 2010 9:12 am
by josh
What your friend is doing is not 'object oriented programming' though, it sounds like its all data and little behavior - but it wouldn't be fair for me to absolutely say that affirmatively without seeing code.