Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6918699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:53:35+00:00 2026-05-27T09:53:35+00:00

OK, I am just trying to get better at making more loosely coupled classes

  • 0

OK, I am just trying to get better at making more loosely coupled classes etc in PHP just to improve my skills. I have a local test database on my computer and for the user table I have a column named “role”. I am trying to build a function that is a general function for getting permissions for a user so it doesn’t depend on a specific task they are trying to do.

When a user tries to do something such as create a new forum topic etc, I want to query the database and if “role” is a certain value, store permissions in a multidimensional array like the following:

$permissions = array(
    'forums' => array("create", "delete", "edit", "lock"),
    'users' => array("edit", "lock")
);

Then I want to be able to search that array for a specific permission without typing the following at the top of every PHP file after a user posts a form by checking isset($var). So if the user is trying to edit a user I want to be able to do something like the following via a class method if possible

if (Class::get_permissions($userID),array($permissionType=>$permission))) {
   // do query
} else {
   // return error message
}

How would be a good way to have a loosely coupled permission checking function that will be able to do something like this? It doesn’t have to be laid out exactly like this but just be loosely coupled so it can be reused and not be bound to a certain task. But I want to be able to have an array of permissions instead of just “admin”,”user”, etc for reusability and so it doesn’t restrict my options down the road. Because I have a bunch of code that is like this right now in the top of my php script files.

if (Class::get_permissions($userID) == "admin") {
   // allow query
} else {
   // return error
}

Thanks for any input to help me get this to where I don’t keep writing the same stuff over and over.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:53:36+00:00Added an answer on May 27, 2026 at 9:53 am

    Your question is a little vague, but I will do my best. You said you’re storing their permissions in an array $permissions.

    public static $permissions = array();
    
    public static function setPermissions($perms)
    {
        if (!is_array($perms)) {
            throw new Exception('$perms must be an array.');
        }
        self::$permissions = $perms;
    }
    
    public static function hasPermission($section, $action)
    {
        if (array_key_exists($section, self::$permissions)
            && in_array($action, self::$permissions[$section])
        ) {
            return true;
        }
    
        return false;
    }
    

    Using that logic, when you read a user’s permissions from the DB, then set the Class::$permissions static var like so:

    Class::setPermissions($permissions);
    // ...
    if (Class::hasPermissions($page, $action)) {
        // user has permission
    }
    

    Note, my code is pretty generic and will have to remain that way until I have more information. For now, I’m assuming your permissions array is using a page section as the index and the array is a list of actions within that page section that the user has access to. So, assuming $page has been set to something like "forums" and the user is currently trying to perform an edit (so $action = 'edit'), the Class::hasPermission() function would return true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just trying to get diff to work better for certain kinds of documents. With
I'm just trying to get more into stl semantics, and converting old loops over
I have making an AJAX wizard using JQuery and I'm trying to get the
Just trying to get my head around what can happen when things go wrong
Just trying to get some rows out of a database and loop through but
I am just trying to get into python, but I've found it very difficult
I am just trying to get my head around various pointer concepts and I
I am just trying to get my head around simple view switching for the
I'm just trying to get started with NDjango but am having issues running a
I am just trying to get Hadoop running on my laptop running 64-bit Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.