How to call parent function from instance of child?

前端 未结 6 2079
青春惊慌失措
青春惊慌失措 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:36

    Technically this is not possible from the "outside" (the public interface) for good reason.

    If you have understood why (otherwise read below), and you actually know what you do, there is no reason to actually not offer the functionality:

    class User extends BaseUser {
       ... 
       function parentDelete(){
           parent::delete();
       }
       ...
    }
    
    user = new User();
    $user->delete();  // will call the overridden delete
    $user->parentDelete();     // want to call parent delete
    

    However if you ever do that, you should know that you have misused inheritance somehow. This must be an exceptional situation as I can not imagine any situation where you actually need that to do at all.

    So try to formulate why you need that functionality so to give yourself better suggestions.

    Why is that bad?

    For a very simple reason: In your software you do not need to know that $user has a parent or not. That is some detail you should not care at all about.

    That will allow you to replace any user-object you have in your software with a childobject of user later on. This is important as you want to change your software over time.

    If you make the internal detail part of the public interface you are robbing yourself the possibility to keep things flexible. Not being flexible is really a bad situation.

提交回复
热议问题