Page 1 of 1

Code encapsulation and include()

Posted: Mon Aug 15, 2005 10:05 am
by evilmonkey
Hello,

I need some opinions on code encapsulation and using included files. I'm working on a user-based project. Each time that a user logs in, a new object (instance of the "user" class) is created. That class contains all the attributes about the user (username, date joined, age, location, etc) and it has all the functions that a logged in user needs. This class is stored in a file called user.class.php. I also have a file called functions.php, which contains all the functions that everyone needs, logged in or not (stuff like simplified SQL_query function, various calculation functions, etc.). Herein lies my question: when is it appropriate to put code into these files? I'll give an example: I have a private messaging system, and my colleague put the function that send messages into the user.class.php, used include() to include that file and called the function as $user->send_message(). I started thinking that it was a little inappropreate because it's such a specific function, that there's no way it can be reused. Therefore, it should be pout with the private messaging system, not with the user class. What are you opinions on this case, and in general? When is it appropreate to put specific functions into a class?

Thanks.

Posted: Mon Aug 15, 2005 10:15 am
by feyd
I'd have to agree with you, except that the user class could carry an instance of the messaging subsystem in it, but probably should just invoke it seperately to save memory and wasted effort opening an object that may or may not get used (within a given user session)

Posted: Mon Aug 15, 2005 10:18 am
by nielsene
Well it sounds like you have a mix of classes and stand-alone functions. That tends to lead to this type of problem. While there are a few functions in most programs that desire "stand-aloneness", its often a sign of poor object design if too many things end up there.

You don't mention your other classes besides User, so we can't tell how your PM system is arranged.

Remember that in OOP, you want to have fairly smaller, "single role" classes. Each class should be an expert in its paticular area without trying to do everything.

I would probably have a "PostOffice" class or something similar and I think most of the PM System code would end up inside their for me -- send_message would be there.

Generally speaking its appropriate to put specific functions into a specific class when that function addresses the class's key role.

Posted: Mon Aug 15, 2005 10:33 am
by evilmonkey
Ah, no, I only have two classes, the user class and the admin class which extends the user class. Everything else is more or less in stand-alone functions and files. Because I'm integrating with a template system too, it's be way too complex to do everything in its own classes. We just call a method or a stand-alone function when the need arises. The overall project is part OOP, but mostly procedural.

Posted: Mon Aug 15, 2005 11:26 am
by nielsene
evilmonkey wrote:Ah, no, I only have two classes, the user class and the admin class which extends the user class. Everything else is more or less in stand-alone functions and files. Because I'm integrating with a template system too, it's be way too complex to do everything in its own classes. We just call a method or a stand-alone function when the need arises. The overall project is part OOP, but mostly procedural.
In that case I'd definitly remove the send_message from the class and stick it in with whatever other functions it logically belongs with. Leaving it within the class will only server to cause more confusion as to what your object model is and lead to the User becoming a God-Class.

Posted: Mon Aug 15, 2005 8:31 pm
by evilmonkey
Thanks for the opinions everyone, I'll take it out of the class. :D