Returning a value in constructor function of a class

后端 未结 8 599
情话喂你
情话喂你 2020-11-27 17:04

So far I have a PHP class with the constructor

public function __construct ($identifier = NULL)
{
 // Return me.
if ( $identifier != NULL )
{
           


        
8条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 17:18

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

提交回复
热议问题