Eval () does not return function results

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

customs::nicknames($data) 

This is a related class:

  class customs extends service { function __construct() { parent::__construct(); } public static function nicknames($data) { return $data; } } 

When I call it like this:

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

The contents of the $ data variable is not returned. Just to try it, I tried using echo and it correctly returns the array in the php error conversion string. Thus, the $data variable is read correctly. But why does he not return anything?

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

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

$data returned.

So what's wrong?

Why can't eval() return the results of a method? How can I solve this problem?

+1
source share
1 answer

Why can't eval () return the results of a method?

Just because you are not returning anything in your eval part.

Documents:

eval () returns NULL if return is not called in the evaluated code, in which case return is returned.


How can I solve this problem?

You can assign a variable ( $merge in this example) to eval . For instance:

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

or return to eval . For instance:

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

Note. Do not use eval in real applications.

Warning from the docs:

The construction of the eval () language is very dangerous because it allows you to execute arbitrary PHP code. Therefore, its use is discouraged. If you have carefully checked that there is no other option than using this design, pay special attention not to go through any user who provided the data in it without properly checking it in advance.


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

Yes, there is (in fact, there is):

  • PHP is a very dynamic language. It has the ability to do the following things with strings :

    • Define and / or get a variable (supported since PHP 4.3). For instance:

       $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

    • Call function (supported by PHP 4.3). For instance:

       // 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

    • Create an instance of the class (PHP 5.0 supported). For instance:

       class MyClass { public function __construct() { echo 'Constructing MyClass'."\n"; } } $className = 'MyClass'; $objFromString = new $className(); // Outputs: object(MyClass)#1 (0) {} var_dump($objFromString); 

      Demo

    • Call a static method (supported by PHP 5.0). For instance:

       class MyClass { public static function staticMethod() { return 'MyClass::staticMethod called'; } } $staticMethodName = 'staticMethod'; // Outputs: string(28) "MyClass::staticMethod called" var_dump(MyClass::$staticMethodName()); 

      Demo

      And from PHP 5.3, the class name can also be specified by a string. Example:

       class MyClass { public static function staticMethod() { return 'MyClass::staticMethod called'; } } $className = 'MyClass'; $staticMethodName = 'staticMethod'; var_dump($className::$staticMethodName()); var_dump($className::staticMethod()); 

      Demo

    • The instance method of invoking an instance of an object (PHP 5.0 supported). For instance:

       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 version

    • Access to object static properties and object instance (supported since PHP 5.0). For instance:

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

      Demo

  • PHP has two functions: call_user_func and call_user_func_array for dynamic calls to functions / methods. Both are well documented, so I will not dwell on it here in detail.
  • Even if all of the above is not enough, PHP 5 comes with a great Reflection API . Unfortunately, there are a few examples in the documentation, but thinking is a pretty big topic to cover here. Basically, it's not very important to use reflection after reading how it works.
+14
source

Source: https://habr.com/ru/post/916185/


All Articles