如何从孩子的实例调用父函数? [英] How to call parent function from instance of child?

查看:95
本文介绍了如何从孩子的实例调用父函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有模型

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

在用户类中
我已被覆盖删除功能

In user Class I have been override the delete function

class User extends BaseUser {
   function delete(){
    }
}

现在,如果我想调用父删除功能....怎么样?

Now, if i want to call parent delete function .... how ?

示例

$user = new User();
$user->delete();  // will call the overridden delete
$user-> ??  ;     // want to call parent delete


推荐答案

从技术上讲,这不是可能来自外部(公共界面)的理由。

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?

原因很简单:在您的软件中,您不需要知道 $ user 有父或母。这是您不应该关心的一些细节。

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.

这篇关于如何从孩子的实例调用父函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆