Check if an object has changed

前端 未结 5 2292
闹比i
闹比i 2021-02-19 21:31

Is there a more native way (e.x. a built-in function) with less userland code to check if an objects property values have changed instead of using one of those methods:

5条回答
  •  你的背包
    2021-02-19 22:17

    There is no built-in method, I'm afraid. The shadow copy approach is the best way.

    A simpler way, if you have control over the class, is to add a modified variable:

    $this->modified = false;
    

    When I modify the object in any way, I simply use

    $obj->modified = true;
    

    This way I can later check

    if($obj->modified){ // Do Something
    

    to check if it was modified. Just remember to unset($obj->modified) before saving content in a database.

提交回复
热议问题