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 6841979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:02:17+00:00 2026-05-27T00:02:17+00:00

The following is an excerpt from some code I wrote to assign the $user->privilege

  • 0

The following is an excerpt from some code I wrote to assign the $user->privilege based on a method from that same class. It seems excessively repetitive, and I am wondering if there is something I can do to make it more readable — given that I haven’t seen this kind of repetition too much in codes I have looked at.

$user -> privileges = $user -> get_privileges ( $user -> username );
  • 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-27T00:02:17+00:00Added an answer on May 27, 2026 at 12:02 am

    It doesn’t look particularly repetitious to me, but it is a little unusual to be assigning an object’s property based on a method outside the class. Instead, this might be better handled inside the object constructor, eliminating the need for you to remember to set the property when coding:

    class User {
        public $username;
        public $privileges;
    
        public function __construct() {
          // setup the user however that's done...
    
          // And assign privileges in the constructor
          $this->privileges = $this->get_privileges();
        }
    
        // In get_privilegs, rather than passing the username property,
        // just access it via $this->username.
        // Unless you need to use this method from time to time outside the class, it can be private
        private function get_privileges() {
          // Get privs for $this->username
        }
    }
    

    And as an alternative to $this->privileges = $this->get_privileges(); called in the constructor, you might just set $this->privileges inside the get_privileges() method. Then you can just call it as $this->get_privileges() in the constructor, no assignment necessary. Either way works.

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

Sidebar

Related Questions

The following is an excerpt from some code that I'm experimenting with: has buffer
I have the following HTML (excerpt from larger code-base) <div class=diary-event ui-corner-all title=[title]> <span
I needed some simple string encryption, so I wrote the following code (with a
Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
I wrote the following method. public T GetByID(int id) { var dbcontext = DB;
When I try to run the following code I get a Conversion from string
I have some serial code like this that computes word concordances i.e. counting collocated
I have some code that throws causes syncdb to throw an error (because it
I have following code: SELECT q21, q21coding AS Description FROM `tresults_acme` WHERE q21 IS
In the following config file excerpt, the WCF service has two endpoints. <service behaviorConfiguration=AtomTcpHub.Behavior

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.