dtt27783 2013-06-10 16:42
浏览 36
已采纳

PHP,Bitwise运算符和使用包含或作为启用选项的参数

Is there a way of defining some variables with true or false and passing them in collectively into a function as a parameter like in C++ like flags to turn sections of a function on or off using the bitwise inclusive or... For example:

// Declare

define( "ADMIN", TRUE);
define( "CLIENT", TRUE);

function Authenticated( $flags )
{
    // Not sure what would go here ? but something like
    // If ADMIN then
    // If CLIENT then
    // If ADMIN | CLIENT then
}

// Call

Authenticated( ADMIN | CLIENT );
  • 写回答

2条回答 默认 最新

  • drmg17928 2013-06-10 16:45
    关注

    You can define constants in your class and make sure their values are separate bits:

    class Authentication
    {
        const ADMIN  = 0x0001;
        const CLIENT = 0x0002;
    }
    
    function Authenticated($flags)
    {
        $adminFlag = $flags & Authentication::ADMIN;
        $clientFlag = $flags & Authentication::CLIENT;
    
        if ($adminFlag && $clientFlag) ...
        else if ($adminFlag) ...
        else if ($clientFlag) ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度