thesavior
02-26-2010, 02:58 PM
I have been asked by a potential employer to make a class that takes another class as a parameter in the constructor. This new class is supposed to look like it has all the methods and variables and such that the passed class has. All the new class is supposed to do is keep track of every function called, their passed parameters and when variables are modified.
EX:
class Small
{
$var = "hello";
public function func2log($param)
{
return $param;
}
}
class Toplayer
{
function __construct($class)
{
}
}
$var = new Toplayer(new Small);
I am told that I should be using reflection and php's runkit to solve this.
I understand reflection, it allows you to check if functions exist in that class before you call them, as well as to get values of variables and constants.
I don't understand why runkit is necessary though, or what it's benefits would be. I'm thinking the only possibility is if reflection can't handle private functions, then you would need runkit to have that functionality.
I have seen a couple examples online where runkit is used to modify behavior of functions, which might have been what the employer was thinking I should do to add the logging. However, I am thinking that if you are using __call() and reflection, I could check if the method exists, log the call, then call the method of the passed class.
Obviously I could ask the employer and I probably should, but I am curious as to what you guys think.
EX:
class Small
{
$var = "hello";
public function func2log($param)
{
return $param;
}
}
class Toplayer
{
function __construct($class)
{
}
}
$var = new Toplayer(new Small);
I am told that I should be using reflection and php's runkit to solve this.
I understand reflection, it allows you to check if functions exist in that class before you call them, as well as to get values of variables and constants.
I don't understand why runkit is necessary though, or what it's benefits would be. I'm thinking the only possibility is if reflection can't handle private functions, then you would need runkit to have that functionality.
I have seen a couple examples online where runkit is used to modify behavior of functions, which might have been what the employer was thinking I should do to add the logging. However, I am thinking that if you are using __call() and reflection, I could check if the method exists, log the call, then call the method of the passed class.
Obviously I could ask the employer and I probably should, but I am curious as to what you guys think.