0

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.

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your information. Actually we are managing user control and we were managing access control with URL . Now We decided we will manage access control one function level . Now i am writing code inside function user has access for functionality or not. If by mistake i will forgot not entered access control inside function . Then any one call function. Even has access right or not . So i want to check inside function there is code for access checking or not. It is old project and there is number of functions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.