php 如何从子实例调用父函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11828463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:14:09  来源:igfitidea点击:

How to call parent function from instance of child?

phpdoctrine

提问by hakre

I have model

我有模型

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

In user ClassI have been override the delete function

在用户类中,我已经覆盖了删除功能

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

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

现在,如果我想调用父删除函数......如何?

Example

例子

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

回答by hakre

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 $userhas a parent or not. That is some detail you should not care at all about.

原因很简单:在你的软件中,你不需要知道它$user是否有父级。这是一些你根本不应该关心的细节。

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.

如果您将内部细节部分作为公共界面的一部分,您就会剥夺自己保持灵活性的可能性。不灵活真的是一个糟糕的情况。

回答by voodoo417

maybe try this

也许试试这个

parent::delete();

回答by j0k

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

delete如果您需要给父母打电话一次,为什么需要延期?

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:

然后你可以使用删除一个对象->customDelete(),你也可以从父级调用 delete 方法,因为你没有覆盖该delete()方法:

$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 reallyneed to override the delete()function, you still can create a kind of parent delete:

如果你真的需要重写这个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

回答by Fluffeh

I think you are looking for the PHP parentfunctions:

我认为您正在寻找 PHP函数:

<?php
class A {
    function example() {
    echo "I am A::example() and provide basic functionality.<br />\n";
    }
}

class B extends A {
    function example() {
    echo "I am B::example() and provide additional functionality.<br />\n";
    parent::example();
    }
}

$b = new B;

// This will call B::example(), which will in turn call A::example().
$b->example();
?>

If your class doesn't have a function called getMyFunction() calling $b->getMyFuction()will call the parent function. If it does, it will call the child function, unless the child function calls the prent function.

如果您的类没有名为 getMyFunction() 的函数,则调用$b->getMyFuction()将调用父函数。如果是,它将调用子函数,除非子函数调用 prent 函数。

回答by Matt

I found this threadon codingforums. What it says (and I tend to agree with them) is that if you have an overridden function/method in a child class, an instance of that object will ALWAYS call the child method.

我在codingforums上找到了这个线程。它所说的(我倾向于同意他们)是,如果您在子类中有一个重写的函数/方法,则该对象的实例将始终调用子方法。

The parentkeyword is akin to $thisor selfand can only be called from within the class itself.

parent关键字是类似于$thisself只能从类内部调用。

If you want a child object to be able to call the parent method, just create another method

如果你想让子对象能够调用父方法,只需创建另一个方法

function parentExample() { 
    parent::example(); 
}

which only calls the parent method.

只调用父方法。

回答by Rikipm

It can be done using call_user_func. Yes, its bad practice, but its possible.

可以使用call_user_func. 是的,这是不好的做法,但有可能。

class BaseUser {
    public function delete()
    {
        echo 'BaseUser deleted';
    }
}

class User extends BaseUser{
    public function delete()
    {
        echo 'User deleted';
    }
}

$instance = new User();

call_user_func(array($instance, 'parent::delete'));

result:

结果:

BaseUser deleted