Correct Implementation of Virtual Functions in PHP?

前端 未结 3 1015
臣服心动
臣服心动 2021-02-12 10:42

at my working place (php only) we have a base class for database abstraction. When you want to add a new database table to the base layer, you have to create a subclass of this

3条回答
  •  一向
    一向 (楼主)
    2021-02-12 11:22

    In PHP all public and protected functions are "virtual". You can prevent functions from being overriden by prepending the final keyword. (Or by making them private, but this is probably a bad idea).

    In the design of the baseclass I would think of behaviors that subclasses would want to affect. I would for example create empty functions like before_update() and after_insert().

    function after_insert() {
      // Virtual
    }
    

    Which the baseclass will call when an update/insert event occurs.

    Maybe an is_valid() function which always returns true in the baseclass, and use the commentblock to describe what the consequences are when a subclass return false.

    Hopefully this would give you some inspiration.

提交回复
热议问题