简体   繁体   中英

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?

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 .

Avoid static methods. As simple as that.

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

$user = new User($id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM