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
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).
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;
}
}