Getting the name of a child class in the parent class (static context)

前端 未结 9 922
栀梦
栀梦 2020-11-29 20:19

I\'m building an ORM library with reuse and simplicity in mind; everything goes fine except that I got stuck by a stupid inheritance limitation. Please consider the code bel

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 20:43

    The problem is not a language limitation, it is your design. Never mind that you have classes; the static methods belie a procedural rather than object-oriented design. You're also using global state in some form. (How does get_row_from_db_as_array() know where to find the database?) And finally it looks very difficult to unit test.

    Try something along these lines.

    $db = new DatabaseConnection('dsn to database...');
    $userTable = new UserTable($db);
    $user = $userTable->get(24);
    

提交回复
热议问题