Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post

    PHP Reflection and Runkit

    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:

    PHP Code:
    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.

  • #2
    New Coder
    Join Date
    Feb 2010
    Location
    UK, North West
    Posts
    36
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Hello,
    It's probably best to define the class type in the contructor.

    PHP Code:
    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:

    PHP Code:
    if($class instanceof $className)
    {
         
    # do magical code here....

    Happy coding,
    Shaun
    Last edited by Shauny_B; 02-27-2010 at 12:19 AM.

  • #3
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    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.

  • #4
    New Coder
    Join Date
    Feb 2010
    Location
    UK, North West
    Posts
    36
    Thanks
    0
    Thanked 5 Times in 5 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •