PHP - static method in class or just a function
I have some dilemma 开发者_开发技巧about controller usage. Like usualy, router (dispatcher or front controller) calls some static method in controller class, eq PageController::showIndexPage(). My controllers usualy have several lines of code and that is usualy model loading, getting data and passing it to view.
My question is - because only one method is executed per http request, should I avoid classes and just create a single function for controller? Sometimes, I realy have several methods per controller, but every time only one method is in usage. I know that is not big deal, but maybe I can get better system? Here is example: My AuthController have methods like showLoginPage(), doLogin(), doLogout()... So, maybe is better idea to avoid class, and put write this controller as several functions in separated files, eq auth/show_login_page.php, auth/do_login.php, and so on? Does this concept have some pros/cons?
Update: Because some users bloat my usage of static method, I'm must defence :) I do not create instance of controller, because there is no need for that. In 99% of cases controller is only used to pass data from model to view. And, there is no need for instance creation for only one method call.That is reason why method is static. Here is example of one of my controllers:
class ArticlesController {
static function showArticle($article_id) {
$article = ArticlesModel::getArticleById($article_id);
View::getInstance()->assignByRef("article", $article);
View::getInstance()->display("articles/one.tpl");
}
static function showAllArticles() {
$articles = ArticlesModel::getAllArticles();
View::getInstance()->assignByRef("articles", $articles);
View::getInstance()->display("articles/all.tpl");
}
}
Static class functions are just pretty like global functions, so no idea in specific why you are using static class functions for your controllers (which is pretty akward), so either change them to standard object methods or global functions altogether.
I suggest you branch your whole app into one branch that replaces all controllers with global functions and the other branch with non-static class functions. Then you can better compare both concepts.
If you don't wanna play around and you're asking for pointers, convert all static class functions into non-static functions, then go on until you run into the next problem.
Don't quite understand the first answer that has been up voted and marked as the correct one. Here is my take on it:
Static methods are like global methods but more organised and contained within a namespace and class. This doesn't mean they are better than objects just because of this definition. They have their usage. If you are working with a class that will maintain a particular state than that is more suited to be a non-static class i.e you should instantiate and use it. If it isn't going to maintain a particular state in said process i.e it just receives parameters does something with it and returns the response it is more suited to be static.
This is my simplistic view of static vs non-static. In your case you may need other things to happen for the smooth working of your controllers. One such thing that is commonly problematic is the fact that static classes don't have constructor as they don't instantiate. This is the only good reason why I think your controller may be instantiated if the parent has a constructor and receives objects that are set for usage across your controllers. I think you've got it right with the static controllers if they are this simple.
As for your question of just having functions or having a class. A class encapsulates your logic into one unit. This is simply a way of static that whatever is in this group strictly belongs to this unit. We came from a world where we used to have plain functions, but that usually deforms quite quickly. Have a look at software entropy, their is little stopping that from happening. Since classes are a reference to what they contain, people use it in that fashion. You can image having 10 requires on top of a file, then you suddenly have a need to a function, a dev may not think much about it and just add it to one of the closest matching file and say ah its all included anyway so it will work. Whereas with classes, you are sort of forced into thinking where to put it, if you put it in the wrong place devs will probably miss it as they are not globally available.
Hope this helps.
I'd say, keep together what belongs together. So use a class. Furthermore, using a class automatically gives some kind of namespace. Just because if you have a method called doSomething()
in several include files, you cannot include more than one of those files. If however the method is put into a "static class", they all still can be included. Since PHP 5.3 you can use "real" namespaces as well, but still the class approach is better.
Or imagine you need a constant. If it is a class, the constant is inside the class scope. If you use functions, you have to pollute the global namespace and only one constant with such a name can be present at a time.
精彩评论