如何在使用字符串连接自动创建PHP代码后执行它?

I have a function that will create a php code that will do a bitwire operation.

this code is generated by function

1 | 2 | 4

what I want to do is return the result of the operation. return (1 | 2 | 4); I tried to use eval() but I get the following error

 Parse error: syntax error, unexpected end of file in ..... : eval()'d code on line 1

The flowing is my code

<?php

class Roles
{

    protected $rolesList = array();

    public function __construct(){

        $this->rolesList = array(
                                'can_use' => 1,
                                'can_view' => 2,      //0000000001
                                'can_update' => 4,    //0000000010
                                'can_insert' => 8,    //0000000100
                                'can_delete' => 16     //0000001000
                            );

    }

    public function mapRoles(){

        if ( func_num_args() == 0 ){
            return false; //no args passed
        }

        $tmp = array();
        $parmeters = func_get_args();

        foreach($parmeters as $arg){
            $tmp[] = $this->rolesList[$arg];
        }

        $cmd = implode(' | ', $tmp);
        return eval($cmd);
    }
}

?>

function that will create a php code that will do a bitwire operation

Usually one does not generate php code in their code.

Instead you might just iterate over array elements and mutually | it:

$val = 0;
foreach($parmeters as $arg) {
    $val |= $this->rolesList[$arg];
}

the other one line implementation that uses an array_reduce might look like this:

$val = array_reduce($data, function($a, $b) { return $a | $b; }, 0);