nitin jain
nitin jain

Reputation: 298

PHP Function Call handler

Is there anything in php for function handler? Like if I call any function in PHP. Function handler automatically call.

function add($i ,$j){
    $k = $i+$j;
    return $k;
}

So if someone call above function, function handler automatically execute. Like we have other function session save_handler, or error_handler in PHP.

Upvotes: 0

Views: 761

Answers (1)

KingCrunch
KingCrunch

Reputation: 131821

No, there isn't. You should use delegation instead

function otherFunction ($i, $k) {
  foobar();
  return add($i, $j);
}

With object orientated design you can also wrap the whole object

class A {
  public function add($i, $j) { return $i + $j; }
}
class B extends A {
  public function add($i, $j) { $this->foobar(); return parent::add($i, $j);
  protected foobar() { /* do something useful */ }
}
// or
class C extends A {
  protected $_inner;
  public function __construct (A $inner) { $this->_inner = $inner; 0
  public function add($i, $j) { $this->foobar(); return $this->_inner->add($i, $j); }
  protected foobar() { /* do something useful */ }
}

Or anonymous functions like

$add = function ($i, $j) {
  foobar();
  return add($i, $j);
};
$add(3, 7);

Upvotes: 1

Related Questions