我有一个变量$ className,它是php中声明的类的名称,我想要
答案 0 :(得分:4)
$obj = new $className();
$var = $className::method();
答案 1 :(得分:3)
1: $ obj = new $ className
2: 的$ className ::的someMethod($参数)
答案 2 :(得分:1)
还有Reflection API。 E.g:
<?php
$rc = new ReflectionClass('A');
// question 1) create an instance of A
$someArgs = array(1,2,3);
$obj = $rc->newInstanceArgs($someArgs);
// question 2) invoke static method of class A
$rm = $rc->getMethod('foo');
if ( !$rm->isStatic() ) {
echo 'not a static method';
}
else {
$rm->invokeArgs(null, $someArgs);
}
class A {
public function __construct($a, $b, $c) { echo "__construct($a,$b,$c)\n";}
public static function foo($a, $b, $c) { echo "foo($a,$b,$c)\n";}
}