How to call parent function from instance of child?

爷,独闯天下 提交于 2019-12-21 07:03:29

问题


I have model

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 ?

Example

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

回答1:


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.




回答2:


maybe try this

parent::delete();



回答3:


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



回答4:


I think you are looking for the PHP parent functions:

<?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.




回答5:


I found this thread on 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.

The parent keyword is akin to $this or self and can only be called from within the class itself.

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.




回答6:


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

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


来源:https://stackoverflow.com/questions/11828463/how-to-call-parent-function-from-instance-of-child

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!