function - Handling PHP Bit Flags -
i have php class method prints out instance table row. want row printed out without name @ start, want row items printed, , on.
i have decided utilize bit flags in order create code more readable, handling of flags looks horrible. first time using bit flags.
i have defined flags @ top of class follows;
define('bat_table_row_player', 1); define('bat_table_row_noplayer', 2); define('bat_table_row_field', 4); define('bat_table_row_all', 7);
the function uses flags looks this;
function tableline($flag=bat_table_row_all) { if(in_array($flag,[1,3,5,7])) // homecoming player cell if(in_array($flag,[2,3,6,7])) // homecoming other cells (except fielding) if(in_array($flag,[4,5,6,7])) // homecoming fielding cells homecoming $rtn; }
so can see, if bat_table_row_all
set, options execute planned.
the problem is, if add together flag reason (therefore setting 8, , 15 - although trivial stands), have rewrite entire function. can't right.
is there improve way write function above have update flag definitions, add together flag execution function?
bitwise operators
function tableline($flag=bat_table_row_all) { if($flag & bat_table_row_player) // homecoming player cell if($flag & bat_table_row_noplayer) // homecoming other cells (except fielding) if($flag & bat_table_row_field) // homecoming fielding cells homecoming $rtn; }
also, can avoid having bump all
flag just:
define('bat_table_row_all', -1);
which has bits set 1.
php function bitflags
No comments:
Post a Comment