dongwuxie5112 2014-01-09 16:41
浏览 30

作为其他类中的方法的结果,将方法作为带有键的数组传递

I have two snippet codes like this:

class User()
{
    var $permission = array();

    function __construct()
    {
        $Permission_Class = new Permission();
        // What to do here with $this->permission 
        // to linked with $Permission_Class->check_permission() 
        // for getting user's permission?
    }
}

class Permission()
{
    var $permission_list = array(
        'can_read'    => 'yes',
        'can_write'   => 'no'
    );

    public function check_permission($permission_name)
    {
        return $this->permission_list[$permission_name];         
    }
}

I want to get the user's permission by simply entering the permission name as an array, especially by this:

$User1 = new User();
$read_permission = $User1->permission['can_read']; // not working currently

Or even more better like this:

$permission = $User1->permission();
var_dump($permission['can_read']); // expected 'yes'

So expected results is 'can_read' in class Permission has 'yes' value. What can I do to get this happen?

Thank you.

  • 写回答

1条回答 默认 最新

  • dongposhi8677 2014-01-09 16:45
    关注

    Easy, just wrap it up!

    class User()
    {
        private $permission = array();
    
        function __construct()
        {
            $this->permission = new Permission();
        }
    
        public function permission($key)
        {
             return $this->permission->check_permission($key);
        }
    }
    
    $user->permission('can_read');
    

    Be sure to check if $key is an existing key. E.G:

    /**
     * $default  would contain the value you want your 
     *           requested permission to have if it does not exist
     */
    public function check_permission($permission_name, $default = NULL)
    {
        return isset($this->permission_list[$permission_name])
            ? $this->permission_list[$permission_name]
            : $default;         
    }
    
    // We want can write to be `FALSE` unless otherwise specified
    $user->permission('can_write', FALSE);
    

    Update after comment

    Event though I advice against using a global permissions array (e.g. what if you'd want to know whether another user has the can_write permission) you could rewrite some of it easily:

    /** class User **/
    public function permission($key = NULL)
    {
         // If no key specified, return entire array
         if($key === NULL)
         {
            return $this->permission->permission_list;
         }
    
         return $this->permission->check_permission($key); 
    }
    

    Then somewhere in your application

    global $permissions;
    $permissions = $user->permission();
    
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装