This article mainly introduces the binary cross permission micro-PHP class, the need for friends can refer to the following
Beautiful point: 1, multiple cross scene allocation; 2, PHP new features-closure use, batch function array_walk/array_flip use; 3, bit operation & use case Use scene: 1, common additions and deletions to check (2 of the N secondary value of storage); 2, certification, Third party account binding, multiple classification selection; code is as follows: <?php class s_allow{ //Declaration usage scenario, Task List public $scene, $case _list=array (); nbsp Declares the specified user role, allowable value, allow listing public $allow _value=0, $allow _list=array (); //initialization scenario, role function __construct ($scene, $allow _value=0) {$this->scene= $scene; $this->case_list=$ This->case_list (TRUE); if ($allow _value) {$this->allow_value= $allow _value $this->allow_list= $this->allow_list ($allow _ Value,true); } //Get all Task List function case_list ($mode =false) {$case _list=config ($this->scene, ' allow '); return $mode? $case _list:array_keys ($case _list); //Get User Task List function allow_list ($allow _value=0, $mode =false) {$buffer =array (); foreach ($this->case_ List as $key => $value) {if ($allow _value & Pow (2, $key-1)) {$buffer [$key]= $value;}} return $mode? $buffer: Array_keys ($buffer); } &NBSP; Computes the Allowed value function Allow_value ($allow _list=null) {if ($allow _list==null) $allow _list= $this->allow_list; array_walk ( $allow _list, function ($value, $key) use (& $allow _list) {$allow _list[$key]=pow (2, $value-1);}); Return Array_sum ($allow _list); //Determine if function Is_allow ($case) {$case _list=array_flip ($this->case_list) is allowed; return (BOOL) ($this- >allow_value & Pow (2, $case _list[$case]-1)); The code is as follows: [Access] 1=add 2=del 3=read 4=list 5=mod 6=detail 7=pub 8=collect 9=like 10=send [cert] 1=email 2 = Tel 3=qq 4=identity_card 5=real_name 6=business_license [bind] 1=qq 2=weibo 3=taobao 4=alipay 5=renren Chat 7=baidu