Why eval() is not able to return the method results?
只是因为你没有在eval部分返回任何内容.
07001
eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned.
How can I solve this issue?
您可以在eval中分配变量(在给定示例中为$merge).例如:
eval('$merge =' . $error['custom'] . ';');
$merge = eval('return '.$error['custom'].';');
注意:不要在实际应用程序中使用eval.
07006
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is
discouraged. If you have carefully verified that there is no other
option than to use this construct, pay special attention not to pass
any user provided data into it without properly validating it
beforehand.
If eval() is dangerous is there another way to read a string as code in a safe way?
是的,有(实际上是):
> PHP是非常动态的语言.它有能力用strings做以下事情:
>定义和/或获取变量(从PHP 4.3支持).例如:
$variableName = 'MyVariable';
// Create new variable with the name defined in variable $variableName
${$variableName} = 'MyValue';
//Outputs: string(7) "MyValue"
var_dump($MyVariable);
//Outputs: string(7) "MyValue"
var_dump(${'MyVariable'});
Demo
>调用函数(PHP 4.3支持).例如:
// Create function with the name defined in variable $functionName
function MyFunction($argument) {
return 'Argument passed is: '.$argument;
}
$functionName = 'MyFunction';
// Outputs:
// string(48) "Argument passed is: Calling MyFunction directly."
var_dump(MyFunction('Calling MyFunction directly.'));
// Outputs:
// string(51) "Argument passed is: Calling MyFunction with string."
var_dump($functionName('Calling MyFunction with string.'));
Demo
>创建类的实例(从PHP 5.0支持).例如:
class MyClass {
public function __construct() {
echo 'Constructing MyClass'."\n";
}
}
$className = 'MyClass';
$objFromString = new $className();
// Outputs: object(MyClass)#1 (0) {}
var_dump($objFromString);
Demo
>调用静态方法(从PHP 5.0支持).例如:
class MyClass {
public static function staticMethod() {
return 'MyClass::staticMethod called';
}
}
$staticMethodName = 'staticMethod';
// Outputs: string(28) "MyClass::staticMethod called"
var_dump(MyClass::$staticMethodName());
而从PHP 5.3类名也可以用string定义.例:
class MyClass {
public static function staticMethod() {
return 'MyClass::staticMethod called';
}
}
$className = 'MyClass';
$staticMethodName = 'staticMethod';
var_dump($className::$staticMethodName());
var_dump($className::staticMethod());
Demo
>调用对象的实例方法(从PHP 5.0支持).例如:
class MyClass {
public function instanceMethod() {
return 'MyClass::instanceMethod called';
}
}
$methodName = 'instanceMethod';
$obj = new MyClass();
// Outputs: string(30) "MyClass::instanceMethod called"
var_dump($obj->$methodName());
Demo
>访问对象的静态和实例属性(从PHP 5.0支持).例如:
class MyClass {
public static $myStaticProperty;
public $myInstanceProperty;
}
$staticPropertyName = 'myStaticProperty';
$instancePropertyName = 'myInstanceProperty';
MyClass::${$staticPropertyName} = 'my static value';
$obj = new MyClass();
$obj->{$instancePropertyName} = 'my instance value';
var_dump(MyClass::${$staticPropertyName});
var_dump($obj->{$instancePropertyName});
> PHP有两个函数:call_user_func和call_user_func_array,用于动态函数/方法调用.两者都有完整的文档,所以我不会在这里详细介绍.
>即使上面的所有内容都不够,PHP 5也配备了很棒的Reflection API.不幸的是,文档中的示例很少,但反射是一个非常大的主题.基本上,在阅读它的工作原理后使用反射并不是什么大问题.