eval()不返回函数结果 [英] eval() does not return the function results

查看:259
本文介绍了eval()不返回函数结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法名称,该名称存储在数据库的一列中,如下所示:

I have a method name that is stored in a column in the DB that looks like this:

customs::nicknames($data)

这是相关的课程:

    class customs extends service {

    function __construct() {
        parent::__construct();
    }

    public static function nicknames($data) {
        return $data;
    }

}

当我这样称呼时:

$merge = eval($error['custom'] . ';');

不返回$ data变量的内容. 只是尝试一下,我尝试了echo,它正确地将数组返回到字符串转换php错误. 因此,正确读取了变量$data.但是为什么它不返回任何东西?

The contents of the $data variable is not returned. Just to give it a try I tried with echo and it is correctly returning the array to string conversion php error. So the variable $data is read correctly. But why does not it return anything?

如果我尝试不使用eval()这样调用此方法:

If I try to call this method without using eval() like this:

$merge = customs::nicknames($data);

$data正确返回.

那怎么了?

为什么eval()无法返回方法结果?我该如何解决这个问题?

Why eval() is not able to return the method results? How can I solve this issue?

推荐答案

为什么 eval()无法返回方法结果?

Why eval() is not able to return the method results?

仅仅是因为您没有在 eval 部分中返回任何内容.

Simply because you don't return anything in your eval part.

文档:

eval() 返回NULL返回传递给 return 的值.

eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned.


我该如何解决这个问题?

您可以在 eval 中分配变量(在给定的示例中为$merge).例如:

eval('$merge =' . $error['custom'] . ';');

return 值手册/zh/function.eval.php> eval .例如:

or return value in eval. E.g:

$merge = eval('return '.$error['custom'].';');


注意:请勿在实际环境中使用 eval 世界应用程序.


Note: Don't use eval in real-world applications.

文档警告:

eval() 语言结构 非常危险 ,因为它允许执行任意PHP代码. 因此它的用途是 灰心丧气. 选项而不是使用此构造,请特别注意 不要通过 任何用户在未正确验证数据的情况下向其提供数据

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.


如果 eval()很危险,是否还有另一种安全方式将字符串读取为代码?

If eval() is dangerous is there another way to read a string as code in a safe way?

是的,(实际上是):

  1. PHP是一种非常动态的语言.它具有使用 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});

演示

这篇关于eval()不返回函数结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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