Victor Bocharsky
Victor Bocharsky

Reputation: 12306

How to intercept PHP function call in my script?

I know that I can intercept method call with __call magic method for objects. But how I can intercept a PHP function (build in PHP or defined by user) call in my part of script?

For example, I have some code:

echo "Hello world";
$s = 0;
$i = $i + 5;
$str = str_repeat('Hi ', $i);
$hash = md5($str);
print $a;

I need to intercept str_repeat() function call and do not execute it and all code after it and return some error (or simply execute die('error');), is it real?

Upvotes: 2

Views: 1949

Answers (1)

Yves Lange
Yves Lange

Reputation: 3974

did you check this : http://www.java2s.com/Code/Php/Class/InterceptingMethodCallswiththecallMethodPHP5Only.htm ?

It's not complicated but you need to write a bit of code.

You can also create your own intercepting function that takes a callback and instead of calling your function/method you can pass it through your own intercepting function.

PSEUDO CODE

function myFakeFunction(...){ ... }

function runFunction($functionName){
  // DO WHAT YOU WANT: INTERCEPTION
  call_user_func($functionName);
}
runFunction("myFakeFunction", ...);

call_user_func in docs.

NOTE: you should care about security issues for both methodologies.

EDIT: I just have a new idea, you can also create a shared object for all the methods that you want to check and use _call on that object. But I'm not sure if that would work correctly.

Upvotes: 2

Related Questions