...

View Full Version : PHP Reflection and Runkit



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.

Shauny_B
02-27-2010, 12:13 AM
Hello,
It's probably best to define the class type in the contructor.



class Small
{
private $var = "hello"; // always make instance variables private

public function func2log($param)
{
return $param;
}

public function GetVar()
{
return (string)$this->var;
}
}

class Toplayer
{
function __construct(Small $class)
{
if(method_exists($class, "func2log"))
{
return (string)(call_user_func(array($class, "func2log"), "some variable..."));
}
}
}

$var = &new Toplayer(new Small());


As you can see, you may even add an array to hold say the methods which you want to call, and/or just contain any relavent methods you're wanting to dynamically call..

However, my code above shows how to do such magical things.


To make sure things are within the correct instances, you can check below:



if($class instanceof $className)
{
# do magical code here....
}


Happy coding,
Shaun

thesavior
02-27-2010, 12:25 AM
The point is that this is supposed to be entirely dynamic, it doesn't matter what class you pass my TopLayer class, it will pretend like it is exactly identical to the other class, EXCEPT that it keeps track of every method called and every variable accessed. That is why I can't define anything like that. My question is about runkit however.

Shauny_B
02-27-2010, 03:09 PM
If you search the methods available, you could look through what I've done and log any method names within a variable and also say in a static array.

Then you can read/write those out into a database or another object...

I'm not entirely sure why you'd want to log every call/variable passed through then make dynamic delegated calls.

Shed some more light and information, I maybe of some help. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum