Laz  Karimov
Laz Karimov

Reputation: 714

How to deal with static methods in PHP OOP?

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?

Upvotes: 1

Views: 121

Answers (2)

deceze
deceze

Reputation: 522005

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.

Read How Not To Kill Your Testability Using Statics.

Upvotes: 3

thwd
thwd

Reputation: 24808

Avoid static methods. As simple as that.

Regarding your comment on the original question, consider the following code example:

$user = new User($id);

Upvotes: 0

Related Questions