I have a super class called LClass. Then I create other classes, which extend LClass.
For example this are classes for tables in database. ( user, order, etc...)
In each of these classes I use some static function getRecordById($id), which returns some array. The difference between these functions, they use different table names for executing. I want to put this static function getRecordById($id) in LClass. The problem is, that function is static, and for this I need some static variables to be set before I do something like $someUser = user::getRecordById($id).
Or any other suggestions?
2 Answers
Programming exclusively using static methods is not object oriented programming, it's "class oriented" programming. And it's essentially the same as procedural code with a sliver of namespacing. Static methods have their use, but it is limited. Static methods should never do the main work of a class.
7 Comments
Laz Karimov
Yes, but in classes can also be some
not static functions. In case of saving some records.deceze
Yes, there can be. There should be. If you start instantiating your objects and use non-static methods, then you've started practicing OOP.
markus
@deceze if you make your answer less extreme by providing an example of a situation where static makes sense, I'll not write my own answer! :)
deceze
@markus I have written about that long and hard in the linked article. It's actually very moderate, not extreme at all. :) Changed the answer a bit to reflect that.
markus
Well, I agree, I try to avoid static... but telling the OP that it's evil may not be the best thing to do. Anyways, your answer is now more balanced for my taste. And nice article, btw.
|
$user = new user;before$someUser = user::getRecordById($id);?$user = new user();I considered, that we can also havegetRecordByName($name)function. So$idin constructor does not always help.