Execute php code inside a variable

2

Good evening personal

You can run this:

<?php

$variavel = 'foreach($v1 as $v2){echo $v2;};';

echo $variavel;

?>

Or is there anyway to run a class inside a variable (a function I know it has):

<?php
$variavel = 'class teste(){function oi(){echo 'oi';}}';
echo $variavel->teste->oi();
?>

something like that.

Thank you in advance

    
asked by anonymous 19.06.2015 / 02:27

3 answers

1
<?php 
$clazz = "class teste(){function oi(){echo 'oi';}}"; 

eval($clazz); 

$variavel = new teste; 

echo $variavel->show();

$variavel->show(); 
?>

Try this.

    
19.06.2015 / 03:21
1

Use the eval () function. I just saw this possibility with this function too. Hugs!

    
01.01.2016 / 06:03
0

The first case can be done as follows:

$funcao = function($variaveis) {
    foreach ($vriaveis as $variavel) {
        echo $variavel . PHP_EOL;
    }
}

$valores = [ 1, 2, 3 ];
$funcao($valores);

The second case is possible with PHP 7 using anonymous classes:

$classe = new class {
    public function oi() {
        echo 'oi' . PHP_EOL;
    }
}

$classe->oi();
    
28.11.2015 / 20:51