How to call parent function from instance of child?

前端 未结 6 2082
青春惊慌失措
青春惊慌失措 2021-02-18 17:57

I have model

BaseUser.class.php
User.class.php
UserTable.class.php

In user Class I have been override the delete function

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-18 18:51

    Why do you need to extend delete if you need to call the parent one time?

    Why not create a custom delete?

    Like:

    public function customDelete()
    {
      parent::delete();
    
      // do extends delete here
    }
    

    Then you can delete an object using ->customDelete() and you also can call the delete method from the parent since you didn't override the delete() method:

    $user = new User();
    $user->customDelete();  // will call the overridden delete
    $user->delete(); // will call the parent (since you didn't override it)
    

    And if you really need to override the delete() function, you still can create a kind of parent delete:

    public function parentDelete()
    {
      return parent::delete();
    }
    

    Then:

    $user = new User();
    $user->delete();  // will call the overridden delete
    $user->parentDelete(); // will call the method that only call the parent
    

提交回复
热议问题