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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:21:58+00:00 2026-05-26T01:21:58+00:00

Currently I have a class called user that I want to create with different

  • 0

Currently I have a class called user that I want to create with different variables, but I think I’m doing it wrong.

Currently I have a class “Unit” with these two functions

public function __construct($table, $id) {
    require_once('database.php');
    require_once('app.php');
    require_once("postmark.php");
    $this->table = $table;
    $this->valid = true;

    if(!$id) {
        $this->valid = false;
    }

    $this->populate($id);
}

public function populate($id) {
    $db = new DB();
    $q = $db->where('id', $id)->get($this->table);
    $resp = $q->fetchAll();
    foreach ($resp as $row) {
        foreach ($row as $key=>$value) {
            if(!is_int($key))
                $this->$key = html_entity_decode($value, ENT_QUOTES);
            if(is_null($value)) {
                $this->$key = null;
            }
        }
    }
    if(count($resp) <= 0) $this->valid = false;
    $verdict = !$db->error;

    $db = null;
    unset($db);

    return $verdict;    
}

And then my “User” class extends it like so

public function __construct($id, $hash = null, $verify = null, $api = null) {
    if($api)
    $value = $this->apiToId($api);
    else if($verify)
    $value = $this->verifyToId($verify);
    else if($hash)
    $value = $this->hashToId($hash);
    else 
    $value = $id;

    parent::__construct("users", $value);
}

But I can’t help but think this is poor in design. A few things I have seen in the past are the use of ampersands, possibly making it so I could do

$user = new User()->fromId($id);

Or

$user = new User()->withHash($hash);

Instead of passing it a long list of null params. That or I could improve the way inheritance works. While I like to think I know what I’m doing with PHP, I’d really like some help looking in the right direction. PHP’s docs are so cumbersome, that I never no where to look, but always find cool useful tools. I’m wondering how I can improve this for more flexibility and structure.

  • 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-26T01:21:59+00:00Added an answer on May 26, 2026 at 1:21 am
    • Move includes to the very top of your php file. Anything that needs to be conditionally included is probably poorly designed.
    • Your unit class should be declared as abstract. This prevents anyone from instantiating a unit. You can only declare subclasses of it.
    • Any functions relating to your class should be declared as methods. Thus, the example given in an answer now-removed is a terrible choice. The function alloc really should be a static function defined in User. Code snippet at bottom.
    • Your init functions should be declared as static and return a new instance of the class. Defining an instance of the class to re-instantiate the class is just a bad idea.
    • Your database connection should use a Singleton pattern. Look it up if you need to.

    Post your full code and comment on this answer if you’d like some help implementing all of this.


    $user  = User::initWithHash($hash);
    
    
    //your create method:
    /**
     * Creates and returns a new instance of the class. Useful
     * @return an instance of User.
     */
    public static function create() {
        return new User();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a couple different user controls that provide the same functionality: three
I'm currently working on an OO PHP application. I have a class called validation
I have a class named Page with a private property currently called _pageData which
I currently have a class and I'm trying to create an easy GUI to
Currently I have a class that looks like this: public class MyClass : IMyClass
Currently I have a class that is extending the ListActivity class. I need to
I currently have my PHP class variables set up like this: class someThing {
We currently have a utilities class that handles a lot of string formatting, date
I currently have a scrolling anchor animation that also adds an active class to
I have a data structure (which is a class) called user_class and I want

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.