Ivan
Ivan

Reputation: 406

Combining two or more function using classes in PHP

I have two php classes.

Class classOne {

  private $stuff;
  public $stuff2;

  public function init(){
    dosomestuff;
  } 

}

&

Class classTwo extends classOne {

  private $stuff;
  public $stuff2;

  public function init(){ #This function is overriding the native classOne method init;
    dosomeotherstuff;
  } 

}

When i call the function init

$obj = new classTwo();
$obj -> init(); #dosomeotherstuff

The PHP interpreter will dosomeotherstuff as anyone would expect, because the classTwo class declared an override on the method init;

Instead, is there a way to combine the effect of the first init and the second, to obtain something like this?

$obj = new classTwo();
$obj -> init(); #dosomestuff, #dosomeotherstuff

Thanks very much

Upvotes: 1

Views: 80

Answers (2)

Blaatpraat
Blaatpraat

Reputation: 2839

In the overridden function you can call the base function:

public function init() {
    parent::init();
    // your normal code
}

Upvotes: 3

Imaginaerum
Imaginaerum

Reputation: 779

use parent into childre method:

Class classTwo extends classOne {

  private $stuff;
  public $stuff2;

  public function init(){ #This function is overloading the native classOne method init;
    parent::init();
    dosomeotherstuff;
  } 

}

Upvotes: 1

Related Questions