评估功能的替代方法是什么? [英] What's alternative of eval function?

查看:142
本文介绍了评估功能的替代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在当前项目中使用eval()就像这样:

I use eval() in my current project like this:

if (class_exists($class_name)) //$class_name depends on user input
    eval($class_name.'::MyStaticMethod()');

仅当存在名称为$class_name的类时才执行

eval(),这有点安全,但是我仍然不认为这是最好的解决方案.

eval() is executed if and only if class with the name $class_name exists so it's kinda safe, but I still don't think that this is the best solution.

在没有eval()的情况下,我可以执行上面的代码吗?

Can I do the same what code above does without eval()?

推荐答案

我最近回答了我的答案的最后一部分可以完美地回答这个问题,对以后的读者来说,比这里提供的答案有用得多.这就是为什么我要回答自己的问题.

I have recently answered this question. The last part of my answer perfectly answers this question and is much more useful for future readers than answers provided here. That's why I am answering my own question.

PHP具有在大多数情况下可以避免使用eval的功能:

PHP has features that gives possibility to avoid using eval in most cases:

  1. PHP是一种非常动态的语言.它具有使用 strings 进行以下操作的能力:

  1. PHP is very dynamic language. It has ability to do following stuff with strings:

  • 定义和/或获取变量(PHP 4.3支持).例如:

  • Define and/or get variable (supported from PHP 4.3). For example:

$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'});

演示

调用函数(PHP 4.3支持).例如:

Call function (supported from PHP 4.3). For example:

// 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.'));

演示

创建类的实例(PHP 5.0支持).例如:

Create instance of class (supported from PHP 5.0). For example:

class MyClass {
    public function __construct() {
        echo 'Constructing MyClass'."\n";
    }
}

$className = 'MyClass';

$objFromString = new $className();
// Outputs: object(MyClass)#1 (0) {}
var_dump($objFromString);

演示

调用静态方法(PHP 5.0支持).例如:

Call static method (supported from PHP 5.0). For example:

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开始,类名称也可以由字符串定义.示例:

And from PHP 5.3 class name can also be defined by string. Example:

class MyClass {
    public static function staticMethod() {
    return 'MyClass::staticMethod called';
    }
}

$className = 'MyClass';
$staticMethodName = 'staticMethod';

var_dump($className::$staticMethodName());
var_dump($className::staticMethod());

演示

对象的调用实例方法(PHP 5.0支持).例如:

Call instance method of object (supported from PHP 5.0). For example:

class MyClass {
    public function instanceMethod() {
        return 'MyClass::instanceMethod called';
    }
}

$methodName = 'instanceMethod';

$obj = new MyClass();
// Outputs: string(30) "MyClass::instanceMethod called"
var_dump($obj->$methodName());

演示

访问对象的静态和实例属性(PHP 5.0支持).例如:

Access static and instance properties of object (supported from PHP 5.0). For example:

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

演示

这篇关于评估功能的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆