phpcodeignitereval

Evaluate a string as PHP code?


I have a string that I want to have PHP read as a piece of code. The reason is that I want to build the set of instructions for PHP in advance, and then execute it later on. Currently I have:

$string = '$this->model_db->get_results()';

And the desired result is:

$string2 = $this->model_db->get_results();

Solution

  • you can have a variable variable/function, but cannot have variable method chains. you can however create a method chain using variable variables/functions.

    Check this page of the php documentation: http://php.net/manual/en/language.variables.variable.php

    it shows the usage of using strings as object or method names. using eval may lead to security vulnerabilities depending on the source of your data.

    $var1 = 'model_db';
    $var2 = 'get_results';
    
    $this->$var1->$var2();