Returning a value in constructor function of a class Returning a value in constructor function of a class php php

Returning a value in constructor function of a class


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}


The best you can do is what Steve has suggested.Never create constructors that do any job other then assigning constructor parameters to the object properties, maybe create some default ones, but nothing else.Constructors are meant to create a fully functional object. Such an object must always work as expected after its instantiation.A user has email, name and probably some other properties. When you want to instantiate a user object, give all those properties to its constructor.Throwing exceptions is not a good way either. An exception is meant to be thrown under exceptional conditions. Asking for a user by email is nothing exceptional, even if you eventualy figure out that no such user exists. Exception could be for example if you ask for a user by email = '' (unless that is a regular state in your system, but id rather suggest emails to be null in those cases).To get all those properties for a user object you should have a factory (or a repository if you prefer) object (yes, an object - it is a bad practice to use static whatever)Private constructor is a bad practice either (you'll need a static method anyway and as i already stated, statics are very bad)

so the result should be something like this:

class User {  private $name;  private $email;  private $otherprop;  public function __construct($name, $email, $otherprop = null) {    $this->name = $name;    $this->email = $email;    $this->otherprop = $otherprop;  }}class UserRepository {  private $db;  public function __construct($db) {    $this->db = $db; //this is what constructors should only do  }  public function getUserByEmail($email) {    $sql = "SELECT * FROM users WHERE email = $email"; //do some quoting here    $data = $this->db->fetchOneRow($sql); //supose email is unique in the db    if($data) {      return new User($data['name'], $data['email'], $data['otherprop']);    } else {      return null;    }  }}$repository = new UserRepository($database); //suppose we have users stored in db$user = $repository->getUserByEmail('whatever@wherever.com');if($user === null) {  //show error or whatever you want to do in that case} else {  //do the job with user object}

See? no statics, no exception, simple constructors and very readable, testable and modificable


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