1: <?php
2:
3: namespace Demo;
4:
5: class NativeEval
6: {
7: public $input;
8: public $result;
9:
10: public function __construct($input)
11: {
12: $semicolonsCount = substr_count($input, ';');
13: if ($input === '') {
14: $this->input = 'return 0;';
15: } elseif ($semicolonsCount == 0) {
16: $this->input = "return {$input};";
17: } elseif ($semicolonsCount == 1) {
18: $this->input = "return {$input}";
19: } else {
20: $lastSemicolon = strrpos($input, ';', -2) + 1;
21: $before = substr($input, 0, $lastSemicolon);
22: $after = substr($input, $lastSemicolon);
23: $this->input = "{$before} return $after";
24: }
25: }
26:
27: public function __invoke()
28: {
29: $result = @eval($this->input);
30: $this->result = $result !== false && $result !== null ? $result : 'Invalid PHP code';
31: }
32: }
33: