I am writing a pretty basic php app and have created a pretty big mess of functions to do various things, e.g. display_user_nav(), display_user_list() etc. I want a way to organize these in a logical way. What I really need is something like a ruby module, but I haven't been able to find a php equivalent. I also feel that from a programming standpoint they don't belong in classes, as I already have a User object to contain information for one user, and don't want to have to create and store a new object whenever I want to use them.
What I am doing now:
display_user_table()
display_user_edit_form()
What I kind of want to be able to do (sort of like Ruby):
User_Functions::Display::Table()
User_Functions::Display::Edit_Form()
Any suggestions are appreciated.
If you are using PHP 5.3+, you have namespaces available.
So you can name your functions (the namespace separator is \
):
User_Functions\Display\Table
User_Functions\Display\Edit_Form
However, it seems like using a class for this wouldn't be a bad idea. If you feel that display functions don't really belong to User
(the same way many people think serialization methods don't make sense in the target objects), you can create a class like this:
class DisplayUser {
private $user;
function __construct(User $u) { $this->user = $u; }
function table() { /* ... */ }
function displayForm() { /* ... */ }
}