PHP constructor returns NULL

I have this code. Is it possible that the constructor of User objects somehow fails, that $this->LoggedUser set to NULL , and the object is freed after the constructor returns?

 $this->LoggedUser = NULL; if ($_SESSION['verbiste_user'] != false) $this->LoggedUser = new User($_SESSION['verbiste_user']); 
+45
null constructor php error-handling
Feb 06 2018-10-06T00
source share
6 answers

Assuming you are using PHP 5, you can throw an exception in the constructor:

 class NotFoundException extends Exception {} class User { public function __construct($id) { if (!$this->loadById($id)) { throw new NotFoundException(); } } } $this->LoggedUser = NULL; if ($_SESSION['verbiste_user'] != false) { try { $this->LoggedUser = new User($_SESSION['verbiste_user']); } catch (NotFoundException $e) {} } 

For clarity, you can wrap this with a static factory method:

 class User { public static function load($id) { try { return new User($id); } catch (NotFoundException $unfe) { return null; } } // class body here... } $this->LoggedUser = NULL; if ($_SESSION['verbiste_user'] != false) $this->LoggedUser = User::load($_SESSION['verbiste_user']); 

As an aside, some versions of PHP 4 allowed you to set $ this to NULL inside the constructor, but I don’t think it was officially authorized, and the β€œfunction” was eventually removed.

+61
Feb 06 '10 at 21:23
source share

AFAIK this cannot be done, new will always return an instance of the object.

What I usually do to get around this:

  • Adding a flag ->valid boolean to an object that determines whether the object was loaded or not. Then the constructor will set the flag

  • Creating a wrapper function that executes the new command returns a new object on success, or if it fails, destroys it and returns false

-

 function get_car($model) { $car = new Car($model); if ($car->valid === true) return $car; else return false; } 

I would be interested to know about alternative approaches, but I do not know.

+10
Feb 06 '10 at 21:10
source share

Think of it like that. When you use new , you get a new object. Period. What you do is a function that searches for an existing user and returns it when it is discovered. This is best expressed by probably a static class function such as User :: findUser (). It can also be expanded when you retrieve classes from a base class.

+4
Feb 06 '10 at 21:16
source share

A factory can be useful here:

 class UserFactory { static public function create( $id ) { return ( filter_var( $id, FILTER_VALIDATE_INT, [ 'options' => [ 'min_range' => 1, ] ] ) ? new User( $id ) : null ); } } 
+4
May 12 '12 at 21:17
source share

When a constructor crashes for some unknown reason, it does not return NULL or FALSE, but throws an exception. Like everything with PHP5. If you do not handle the exception, then the script will stop executing with the error "Disable exception."

+3
Feb 06 '10 at 21:11
source share

maybe something like this:

 class CantCreateException extends Exception{ } class SomeClass { public function __construct() { if (something_bad_happens) { throw ( new CantCreateException()); } } } try{ $obj = new SomeClass(); } catch(CantCreateException $e){ $obj = null; } if($obj===null) echo "couldn't create object"; //jaz303 stole my idea an wrap it into a static method 
+3
Feb 06 '10 at 21:17
source share



All Articles