Code encapsulation and include()

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Code encapsulation and include()

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Thanks for the opinions everyone, I'll take it out of the class. :D
Post Reply