0

I have the following:

class bar {

    function __construct(){

         // set $a_from_fire to $a from fire() method arg
         // set $b_from_fire to $b from fire() method arg
    }
}

class foo extends bar {

    function fire ($a, $b){

    }
}

I need to set $a_from_fire and $b_from_fire using the arguments from foo->fire()

So if I do this:

$test = new foo;
$test->fire(1, 2);

These vars will be set:

$a_from_fire == 1; // true
$b_from_fire == 2; // true
phirschybar
  • 8,357
  • 12
  • 50
  • 66
  • your question is not very clear :s – Halayem Anis Jan 02 '15 at 19:13
  • 2
    I'm not sure exactly what you're trying to do. Can you at least show some non-working code or further description that explains what you want? Also, where is `$a_from_fire` declared? – Reed Jan 02 '15 at 19:13
  • I just added some clarification. I am trying to debug a set of classes which are running as jobs in a message queue. They all extend one parent class so I am hoping I can log the arguments passed to each of the child class methods by simply adding a method to the parent class in order to capture the arguments. – phirschybar Jan 02 '15 at 19:22
  • perhaps this can be done with something other than __construct ? – phirschybar Jan 02 '15 at 19:23
  • I also ran across [this post](http://stackoverflow.com/a/3386948/802469) yesterday which could potentially help, but it didn't sound like it would be helpful given your particular case (since you'd have to rewrite some code outside the parent class). – Reed Jan 03 '15 at 14:09

2 Answers2

0

This isn't possible because __construct() is called when the object is first instantiated, so fire($a, $b) will always run after __construct()

If you just want to set the variables when fire() is called, simply do:

class bar {
    protected $a_from_fire;
    protected $b_from_fire;
}

class foo extends bar {
    public function fire($a, $b) {
        $this->a_from_fire = $a;
        $this->b_from_fire = $b;
    }
}
Populus
  • 7,470
  • 3
  • 38
  • 54
0

I don't think you can do it in any "correct" way. My first thought was to use __call, but that of course is only called for undefined functions.

And there's not really any way to dynamically rename the methods, unless you're already using RunKit. (not that I know of or could find anyway).

If it's purely for debug purposes, you could set your own class autoloader to pre-process the files, change the method names, and then use the __call magic method on your parent class.

spl_autoload_register(function($class){
       $hackPath = '/home/_classes/'.$class;
       if (!file_exists($hackPath)){
           $realPath = '/home/classes/'.$class;
           $file = file_get_contents($realPath);
           $processedContent = //use regex or something to prepend all function names with an _.
           file_put_contents($hackPath,$processedContent);
       }


       require_once $hackPath;
    });

Then in your parent class

class parent {

    public function __call($funcName,$arguments){

       $this->myLogFunc($funcName,$arguments);
       //since you prepended with an underscore
       return call_user_func_array('_'.$funcName,$arguments);

    }

This is a terrible way to do what you're asking, but it could work. The pre-processing of files might be slow, but you'd only need to do it if the originals changed (you can use filemtime to check if it's changed).

Reed
  • 14,703
  • 8
  • 66
  • 110