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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:51:29+00:00 2026-06-17T10:51:29+00:00

I’ve written a user class based on other supposedly high quality, secure classes I

  • 0

I’ve written a user class based on other supposedly high quality, secure classes I found online (although mixing some of them since, from what I’ve learned, none was actually that secure). The thing is, inside my script that initializes the different objects, I’ve got a too long snippet for the $User object. Other objects require as little as $Browser = new Browser(); or $_ = new Translate ($DB, $User->get('Language'));. So, it feels kind of ugly to have all this code suddenly here:

  /* USER. Handles user data and login/logout/register. */
  $User=new User($DB, Configuration::get('SiteKey'));
  if (isset($_POST['logout']))
    $User->logout();
  else if (isset($_POST['login']) && !$User->login($_POST['email'], $_POST['password']))  // If user tries to login
    $Error->set ('Banner', 'Username or password incorrect. Please try again.');
  else if (isset($_SESSION['email']) && isset($_SESSION['session']))
    $User->loginSession ($_SESSION['email'], $_SESSION['session']);
  else if (isset($_POST['register']))
    $User->add ($_POST);

  /* Language */
  if (!empty ($_POST['lang']) && in_array($_POST['lang'],Configuration::get('SupportedLanguages')))
    {
    $User->set('language', $_POST['lang']);
    $_SESSION['language'] = $_POST['lang'];
    }
  if (!$User->get('language'))
    $User->set('language', Configuration::get('DefaultLanguage'));

I’m not sure where all this code is supposed to be, should I put this inside the User’s class __constructor() or this is it supposed to be outside as it is now?

It’s better to pass the $_POST than using it directly inside, which might affect the answer (a __constructor() with too many variables passed isn’t also really good).

I don’t think the code within the class is so relevant as to append it here, but if you want to see it I just made it publicly available in my github.

  • 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-06-17T10:51:30+00:00Added an answer on June 17, 2026 at 10:51 am

    I’m not sure where all this code is supposed to be, should I put this
    inside the User’s class __constructor() or this is it supposed to be
    outside as it is now?

    I wouldn’t put it in the constructor. Whatever script or class you have this code in looks to perform a number of responsibilities. You could consider following the single responsibility principle and separate out this code into other classes such as an authentication class, a session class and a user factory that can build the user object. A brief and by no means carefully architectured example:

    class User
    {
    
    }
    
    class Session
    {
    
    }
    
    class UserFactory
    {
      public function create($vars, ...)
      {
      //build user...
      return $user;
      }
    }
    
    class Authenticate
    {
      public function __construct($userFactory)
      {
        //...
      }
    
      public function Login($username, $passwd)
      {
        //do stuff...
        return $userFactory->create($vars, ...);
      }
    
      public function AuthenticateSession($session)
      {
        //...
       return $userFactory->create($vars, ...);
      }
    }
    

    a __constructor() with too many variables passed isn’t also really
    good

    Well you can always group variables into a collection object and pass that in the constructor. For example language and time settings could be grouped into a culture object.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a small JavaScript validation script that validates inputs based on Regex. I
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need to clean up various Word 'smart' characters in user input, including but
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.