Returning a value in constructor function of a class, PHP?

Even if you could make it fail sometimes, you will still need to check after calling the constructor to ensure it actually did construct, and in those lines, you could just call the ->loadUser() and pass the results into the constructor A good hint someone one told me, "always give the constructor what it needs to build the object, don't make it go looking for it public function __construct ($emailInTheDatabase, $otherFieldNeeded) { $this->emailAddress = $emailInTheDatabase; $this->otherField = $otherFieldNeeded; }.

Even if you could make it fail sometimes, you will still need to check after calling the constructor to ensure it actually did construct, and in those lines, you could just call the ->loadUser() and pass the results into the constructor. A good hint someone one told me, "always give the constructor what it needs to build the object, don't make it go looking for it." public function __construct ($emailInTheDatabase, $otherFieldNeeded) { $this->emailAddress = $emailInTheDatabase; $this->otherField = $otherFieldNeeded; }.

The constructor is suppose to create an object. Since in php booleans are not considered to be objects the only option is null. Otherwise use a workaround i.e.

Write a static method which creates the actual object. Public static function CheckAndCreate($identifier){ $result = self::loadUser(); if($result === true){ return new EmailClassNameHere(); }else{ return false; } }.

A constructor cannot return anything but the object it is attempting to create. If the instantiation doesn't complete properly, you'll be left with a class instance full of NULL properties as you've discovered. If the object loads in an incomplete or error state, I would suggest setting a property to indicate that.

// error status property public $error = NULL; public function __construct ($identifier = NULL) { // Return me. If ( $identifier! = NULL ) { $this->emailAddress = $identifier; if (!$this->loadUser() ) { // registered user requested , but not found!

$this->error = "user not found"; } } When instantiating the object then, you can check if it has an error status: $obj = new MyObject($identifier); if (!empty($obj->error)) { // something failed. } Another (perhaps better) alternative is to throw an exception in the constructor, and wrap the instantiation in a try/catch.

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 }.

Thanks for all the comments and solutions. Here is what I done to fix the problem: (i hope it helps others) // Setup the user ( we assume he is a user first. Referees, admins are considered users too ) try { $him = new user ($_emailAddress); // check the supplied password $pass_ok = $him->auth($_Password); // check the activation status $active_ok = $him->makeActive(); } catch (Exception $e_u) { // try the groups database try { $him = new group ($_emailAddress); // check the supplied password $pass_ok = $him->auth($_Password); //var_dump ($pass_ok); // check the activation status $active_ok = $him->makeActive(); } catch (Exception $e_g) { // email address was not in any of them!

$pass_ok = false; $active_ok = 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? Thank you all for the answers.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions