简体   繁体   中英

How to call a overrided method in child, on the parent class

I've got a class

class foo {

  function do_something() {
      load();
  }

  function load() {
     //things
     echo 'load from foo';
  }
}

And another class that extends foo (a child class):

class bar extends foo {

  function load() {
     //things
     echo 'load from bar (child)';
  }

}

And then:

$obj = new bar();

What I want to know is how can I call $obj->do_something() such that the method uses the child 'load' method instead of the method declared in the foo class.

So, I want the output to be:

$obj->do_something();

Output: load from bar (child)

Is this possible with PHP?

Thanks!

You need to qualify the object context with $this . Unlike some other languages, PHP requires that you qualify the instance explicitly with $this for all method calls from within an object.

class foo {
    function do_something() {
        // load();
        $this->load();
    }
    function load() {
        echo 'load from foo';
    }
}

class bar extends foo {
    function load() {
        echo 'load from bar (child)';
    }
}

$obj = new bar();
$obj->do_something();

In your code ( merely calling load() ) the language was looking for a globally defined function named load , which of course wouldn't work ( or in worse cases, would but incorrectly )

As another answer points out, you must similarly qualify static methods though the use of self or static ( which I would suggest you read up on to understand the binding differences )

From an inheriting class, in an overriding method, you can also use parent to invoke the parent classes' definition of the method:

class bar extends foo {
    function load() {
        parent::load(); // right here
        echo 'load from bar (child)';
    }
}

This will invoke the parent's version of the load method, and continue execution with the child classes' definition.

I would recommend use abstract function if you want to have such a tight coupling of methods. it's going to require high maintenance and you are creating some tight couplings between classes.

i would recommend creating an abstract function in the parent that each of the children will implement with it's own logic.

Even then if you want change your parent class function to this

function do_something(){
    if(method_exists($this, 'test')){
        $this->test();
    }
}

you can use abstract class and abstract method:

abstract class Foo{
   function test() {
      $this->method();
   }
   abstract function method();
}
class Test extends Foo {
   function method() {
      echo 'class Test';
   }
}
$test = new Test();
$test->test();

Try this

class foo {

  function do_something() {
      static::load();
  }

  function load() {
     //things
     echo 'load from foo';
  }

}

class bar extends foo {

  function load() {
     //things
     echo 'load from bar (child)';
  }

}

$obj = new bar;
$obj->do_something();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM