返回类的构造函数中的值

时间:2011-07-27 18:47:10

标签: php oop class constructor

到目前为止,我有一个带有构造函数的PHP

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

loadUser的功能是在数据库中查找特定的电子邮件地址。 当我将标识符设置为某些电子邮件时,我确定它不在数据库中;第一个IF被传递,然后进入第一个ELSE。这里构造函数应该返回FALSE;但相反,它返回具有所有NULL值的类的对象!

我该如何防止这种情况?谢谢

编辑:

谢谢大家的答案。那很快!我看到OOP方式是抛出异常。所以抛出一个,我的问题改变了我应该怎么做的例外? php.net的手册非常令人困惑!

    // 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 !!  
        }
    }

8 个答案:

答案 0 :(得分:68)

构造函数不会获得返回值;它们完全用于实例化课程。

如果不重组你已经在做的事情,你可以考虑在这里使用例外。

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

现在,您可以这种方式创建新用户。

try {
    $user = new User('user@example.com');
} catch (Exception $e) {
    // unable to create the user using that id, handle the exception
}

答案 1 :(得分:8)

构造函数假设是创建一个对象。因为在php中,booleans不被认为是对象,所以唯一的选择是null。否则使用解决方法,即编写一个创建实际对象的静态方法。

public static function CheckAndCreate($identifier){
  $result = self::loadUser();
  if($result === true){
    return new EmailClassNameHere();
  }else{
    return false;
  }
}

答案 2 :(得分:5)

史蒂夫建议你做的最好的事情。 永远不要创建执行任何工作的构造函数,然后将构造函数参数分配给对象属性,也可以创建一些默认值,但不能创建任何其他工作。 构造函数旨在创建一个功能齐全的对象。在实例化之后,这样的对象必须始终按预期工作。 用户有电子邮件,名称和可能的其他一些属性。如果要实例化用户对象,请将所有这些属性提供给其构造函数。 抛出异常也不是一个好方法。在例外条件下抛出异常。通过电子邮件询问用户并不是特例,即使您最终发现没有这样的用户存在。例外情况可能是,例如,如果您通过email =''询问用户(除非这是您系统中的常规状态,但在这些情况下,id建议电子邮件为空)。 要获取用户对象的所有属性,您应该有一个工厂(或者如果您愿意,还有一个存储库)对象(是的,一个对象 - 使用静态的不好做法) 私有构造函数也是一种不好的做法(无论如何你都需要静态方法,正如我已经说过的那样,静态非常糟糕)

所以结果应该是这样的:

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
}

请参阅?没有静态,没有例外,简单的构造函数和非常易读,可测试和可修改的

答案 3 :(得分:4)

构造函数除了正在尝试创建的对象外,不能返回任何内容。如果实例化没有正确完成,您将留下一个充满NULL属性的类实例,如您所发现的那样。

如果对象以不完整或错误状态加载,我建议设置一个属性来指示。

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

然后,在实例化对象时,您可以检查它是否有错误状态:

$obj = new MyObject($identifier);
if (!empty($obj->error)) {
   // something failed.
}

另一种(可能更好的)替代方法是在构造函数中抛出异常,并将实例化包装在try/catch中。

答案 4 :(得分:3)

为什么不简单地将结果传递给构建对象所需的构造函数,而不是尝试使构造函数有时失败?

即使你有时会让它失败,你仍然需要在调用构造函数后检查以确保它实际构造,并且在这些行中,你可以只调用 - > loadUser ()并将结果传递给构造函数。

有人告诉我一个好的提示,“总是给构造函数提供构建对象所需的内容,不要让它去寻找它。”

public function __construct ($emailInTheDatabase, $otherFieldNeeded)
{
    $this->emailAddress = $emailInTheDatabase;
    $this->otherField = $otherFieldNeeded;
}

答案 5 :(得分:0)

感谢所有评论和解决方案。这就是我为解决这个问题所做的工作:(我希望它有助于其他人)

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

答案 6 :(得分:0)

我不会在构造中加入太多东西。您应该考虑创建User(工厂)的静态功能,而不是将所有内容都放在构造函数中。因此,您仍然可以使用您的用户对象,而无需隐式调用加载函数。这样可以省去你的痛苦。

public function __construct(){}

public function setIdentifier($value){
    $this->identifier = $value;
}

public function load(){
    // whatever you need to load here
    //...
    throw new UserParameterNotSetException('identifier not set');
    // ...
    // if user cannot be loaded properly
    throw new UserNotFoundException('could not found user');
}

public static function loadUser($identifier){
    $user = new User();
    $user->setIdentifier($identifier);
    $user->load();
    return $user;
}

样本用法:

$user = new User(); 
try{
    $user->setIdentifier('identifier');
    $user->load();
}
catch(UserParameterNotSetException $e){
    //...
}
catch(UserNotFoundException $e){
    // do whatever you need to do when user is not found
}

// With the factory static function:
try{
    $user2 = User::loadUser('identifier');
}
catch(UserParameterNotSetException $e){
    //...
}
catch(UserNotFoundException $e){
    // do whatever you need to do when user is not found
}

答案 7 :(得分:0)

我真的很惊讶4年来没有22k观众建议创建私有构造函数和尝试创建这样的对象的方法:

class A {
    private function __construct () {
        echo "Created!\n";
    }
    public static function attemptToCreate ($should_it_succeed) {
        if ($should_it_succeed) {
            return new A();
        }
        return false;
    }
}

var_dump(A::attemptToCreate(0)); // bool(false)
var_dump(A::attemptToCreate(1)); // object(A)#1 (0) {}
//! new A(); - gives error

这样你就得到一个对象或者假(你也可以让它返回null)。抓住这两种情况现在非常容易:

$user = User::attemptToCreate('email@example.com');
if(!$user) { // or if(is_null($user)) in case you return null instead of false
    echo "Not logged.";
} else {
    echo $user->name; // e.g.
}

您可以在此处进行测试:http://ideone.com/TDqSyi

我发现我的解决方案比投掷和捕获异常更方便。