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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:30:12+00:00 2026-05-19T02:30:12+00:00

I am building an user class that manage the creation, deletion and modification of

  • 0

I am building an user class that manage the creation, deletion and modification of a generic user. My class should be used in this way:

# creation
user::create($username, $password, $email); // Does not need of $id

# modification
$u = new user($id);
$u->edit('password', $new_password);

# deletion
$u->delete();

Basically the class contain a static method create() that obliviously does not require the used id as argument. After the creation you can gather user infos and manage the user creating an instance of the class user and set as argument the $id of the user.
Is that a good design or should i create something like:

# creation
$users = new genericUserMethod();
$users->create($username, $password, $email);

# modification
$u = new specificUser($id);
$u->edit('password', $new_password);

# deletion
$u->delete();

…Creating 2 different classes. Or is there any other way?

  • 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-19T02:30:13+00:00Added an answer on May 19, 2026 at 2:30 am

    This could be an approach:

    class User {
        private $id;
        private $name;
        //more fields here
    
        public function __construct($id = null) {
            $this->id = $id;
            if(!is_null($this->id)) {
                $this->load_user_data();
            }
        }
    
        protected function load_user_data() {
            //select from DB where id = $this->id and populate fields
        }
    
        public function save() {
            //if $this->id is null insert the user details in DB and populate $this->id with new user's id
            //else update DB with field (optionally check what has changed and update only if necessary)
        }
    
        public function delete() {
            //delete user if $this->id is not null
        }
    
        //fields getters and setters here as needed
    
    }
    

    Usage sample:

    $mary = new User(); //fresh new user
    echo $mary->getId(); //returns null as this user is not inserted.
    $mary->setName('mary');
    $mary->save(); //insert user with name mary in the DB
    echo $mary->getId(); // returns an id as this user is now inserted
    
    $john = new User(2); // we assume there was a user john in DB with id = 2
    echo $john->getName(); //echoes 'john' if this was his name in DB
    

    You can even define static methods in the class like getActiveUsers() that returns an array with the active users for example…

    Note: This is intended for quite simple needs, in case you require to do dome complex things I would recommend you to use an ORM library as pointed @What is the question

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

Sidebar

Related Questions

When looking beyond the RAD (drag-drop and configure) way of building user interfaces that
So I have this calculator I'm building that accepts user variable inputs like let
I am building a helper class that would allow me to manage facebook account
I'm currently building a user control that displays a message when a Repeater is
I am currently building a user admin area for my site that would require
Hi all im building a web user interface. In this interface i use popup
What are some recommended WordPress plugins that make building an online user manual more
I'm building a Zend-based Web app that will permit third-party extensions. Essentially, this application's
Currently I have three models in Django that create a circular reference: A User
I'm building a simple PHP class that will allows you to enter your twitter

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.