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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:11:47+00:00 2026-05-24T00:11:47+00:00

So far I have a PHP class with the constructor public function __construct ($identifier

  • 0

So far I have a PHP class with the constructor

public function __construct ($identifier = NULL)
{
 // Return me.
if ( $identifier != NULL )
{
  $this->emailAddress = $identifier;
  if ($this->loadUser() )
    return $this;      
  else
  {
// registered user requested , but not found ! 
return false;
  }
}

the functionality of loadUser is to look up the database for a particular email address.
When i set the identifier to some email that i’m sure it’s not in the database; the first IF is get passed, and goes to the first ELSE. here the constructor should return FALSE; but instead, it returns an object of the class with all NULL values !

how do i prevent this? thanks

EDIT:

thank you all for the answers. that was quite fast ! I see that the OOP way is to throw an Exception. So a throw one, my question changes that what should i do with the exception??
php.net’s manual is pretty confusing !

    // Setup the user ( we assume he is a user first. referees, admins are   considered users too )
    try { $him = new user ($_emailAddress);
    } catch (Exception $e_u) { 
      // try the groups database
      try { $him = new group ($_emailAddress); 
      } catch (Exception $e_g) {
          // email address was not in any of them !!  
        }
    }
  • 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-24T00:11:48+00:00Added an answer on May 24, 2026 at 12:11 am

    Constructors don’t get return values; they serve entirely to instantiate the class.

    Without restructuring what you are already doing, you may consider using an exception here.

    public function __construct ($identifier = NULL)
    {
      $this->emailAddress = $identifier;
      $this->loadUser();
    }
    
    private function loadUser ()
    {
        // try to load the user
        if (/* not able to load user */) {
            throw new Exception('Unable to load user using identifier: ' . $this->identifier);
        }
    }
    

    Now, you can create a new user in this fashion.

    try {
        $user = new User('user@example.com');
    } catch (Exception $e) {
        // unable to create the user using that id, handle the exception
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far this is what I have to work with: <div class=toplist> <div class=toplist_left></div>
This is what I have so far: myArray.map!{ rand(max) } Obviously, however, sometimes the
I am very new to PHP, so far I have been using the following
I have a php program i have developed on the internet. So far i
I'm very new to PHP so far and have been trying to learn it.
I have this html: <div id=signbtn> <a id=login-link class=btnsignin title=Login>Sign in</a> </div> Upon successful
I have this in my bootstrap: protected function _initAutoload() { $this->_auth = Zend_Auth::getInstance(); $this->_acl
I have the following form that has 2 selects: <form id=form class=form_visitar method=post action=ajax/selects.php>
So far I have encountered adjacency list, nested sets and nested intervals as models
So far i have got the code below which works lovely when trying an

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.