Reputation: 117
In MVC, some controller functions require accessing the model before rendering a view, whereas others simply load the view.
In a blogging app, I have two functions:
public function add_post()
{
// loads the view for adding a new post
}
public function add_new_post()
{
// queries database and inserts post
}
I'm used to using Java, where all methods have a highly suggestive verb that describes what the method is supposed to do. So, instead of add_post(), I would use add_post_view(), and instead of add_new_post(), I would be able to use add_post().
But I'm afraid that adding "view" at the end seem redundant - but aside from that, I'd like to know what the convention is for those who like to have highly readable code. How would you go about rewriting the names of these two functions?
Upvotes: 3
Views: 1812
Reputation: 6588
Its good practice to use 'thin' controllers, i.e. not to add too many methods on any single controller. With that in mind, I usually have a well named controller with only one (two max) public methods on there. Usually corresponding to GET and POST.
Upvotes: 1
Reputation: 36937
For php, in general, there is no specialized naming convention or should I rather say standardized. Working for several companies over the years I have seen it all from thisismyfunction to thisIsMyFunction to this_is_my_function..
My personal choice though, is if its internal functionality (models or controllers alike) that aren't AJAX ready I tend to use the underscore method with a couple comments under or above each function to give me an idea. But for functions I intend to use with AJAX such as in a controller I will usually go the camel-case route. Generally speaking its all about preference on your side of it. If your more comfortable with underscore use that, if not then use camel case, or whatever suits ya.
If this is going to be a team project, or potentially a project where either a team will come aboard, or where you expect to be bought out. I'd say go for what ever is easiest learning curve wise, last thing you want to do is bring others in or sell a product that your going to have to spend long times training people how to read through your code. Rather than collect and go, or just have them start building away.
Upvotes: 3