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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:17:10+00:00 2026-06-03T20:17:10+00:00

Consider the following, simple class constructor. (note that I am obviously not including all

  • 0

Consider the following, simple class constructor. (note that I am obviously not including all methods that are referenced.

// Initialize User class.
public function __construct($user_id = NULL)
{
    // If user is loaded (and a user ID is provided)
    if ($user_id)
    {
        // If user is authorized.
        if ($this->authorized($user_id))
        {
            // Load user information.
            $this->info = $this->load($user_id);
        }
        else
        {
            // Return an empty (nonexistent) user.
            return NULL;
        }
    }

    // If user is loaded (and no user ID is provided)
    else
    {
        // Create a new user.
        $new_user = create_user();

        // Return the new user's ID.
        return $new_user;
    }
}

My question is this: is my method of returning values here wrong? My friend insists that a constructor should ALWAYS return an object NO MATTER WHAT. However, the way I have it laid out here seems much simpler, and is much easier to work with. (If I am making a new user, then I get his ID right off the bat. If I am loading an existing user, I immediately have access to her/his information)

If it IS wrong, why? Why is this bad?

  • 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-03T20:17:12+00:00Added an answer on June 3, 2026 at 8:17 pm

    What you’re trying to do simply doesn’t work, the constructor will return the new instance of User anyway, even when you try to return null.

    For example, this:

    class User {
      function __construct() {
        return null;
      }
    }
    var_dump(new User());
    

    will print:

    object(User)#1 (0) {
    }
    

    http://codepad.org/0IdJydkY

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

Sidebar

Related Questions

Consider the following simple example class, which has a property that exposes a modified
Please consider the following simple use case: public class Foo { public virtual int
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the following simple map: class MyCoolMap : public unordered_map<const char *, const char
Consider following simple DAO example: public abstract class DAOFactory { public abstract AccountDAO getAccountDAO();
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
Consider the following simple example: [DataContract({0}Base)] public class Base<T> where T : Entity<T> {
Consider the following simple polymorphism ... class Parent { public: someFunc() { /* implementation

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.