我认为这就是你要找的:
class MyTestClass
{
public $att1, $att2, $att3;
function __construct($att1, $att2, $att3)
{
$class = new ReflectionClass('MyTestClass');
$construct = $class->getConstructor();
foreach ($construct->getParameters() as $param)
{
$varName = $param->getName();
$this->$varName = ${$param->getName()};
}
}
function confirm()
{
print "att1 = ". $this->att1 . "<br>";
print "att2 = ". $this->att2 . "<br>";
print "att3 = ". $this->att3 . "<br>";
}
}
// Example for non-class functions
function init($a, $b, $c)
{
$reflector = new ReflectionFunction('init');
foreach ($reflector->getParameters() as $param) {
print $param->getName(). " = ".${$param->getName()}."<br>";
}
}
init ("first", "second", "third");
$testClass = new MyTestClass("fourth", "fifth", "sixth");
$testClass->confirm();
结果:
a = first
b = second
c = third
att1 = fourth
att2 = fifth
att3 = sixth