php - Is this an acceptable use of eval()? -


i'm trying use imploded string operator. , php novice, eval() option works, far.

i've read other questions/answers on eval(). in cases, people trying allow actual code input user, not i'm doing here.

here is:

/*  * user choices multiple-select customizer custom control.  * setting's choice values names of wordpress conditional tags.  * each choice set given 'key'=>'label' not 'key'=>'function'  * i'm limited using strings here, e.g. is_front_page, is_singular.  * result linear array, e.g. array( is_front_page, is_singular ).  */ $conditions = get_theme_mod( 'theme_setting' );  /*  * here's trouble begins.  * need append `()` each array value using array_walk. */ array_walk( $conditions, function(&$value, $key) { $value = $value . '()'; });  /*  * next implode array values insert or operator  * instead of "is_front_page, is_singular", i'd end  * "is_front_page() || is_singular()"  */ $conditions = implode( ' || ', $conditions );  /* previous step not usable, in novice experience, hence eval() */ eval( '$conditions =  ' . $conditions . ';');  /* eval makes following usable */ if( $conditions ) {     // stuff here } 

i'm hoping acceptable because i'm not allowing code input user , theme setting static can't $conditions === true workaround.

even if acceptable, please let me know if have advice on how improve it.

nooo... thinking broadly. have array of function names , jumped far executing them freeform code.

actually function names valid callbacks , safer , easier execute call_user_func(). array_map('call_user_func', $conditions) turn callbacks return values.

but note desired condition of or type. don't need run every callback, need execute them until first true one.

this can expressed as:

$result = array_reduce( $callbacks, function ( $carry, $callback ) {     return $carry ?: (boolean) call_user_func( $callback ); }, false ); 

Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -