PHP constructor to return a NULL

PHP programming language
Post Reply
User avatar
Harrietta
Posts: 1
Joined: Sun Feb 04, 2018 12:43 pm

PHP constructor to return a NULL

Post by Harrietta » Tue Feb 13, 2018 5:41 pm

I have this code. Is it possible for a

Code: Select all

 User 
things fabricator to somehow fail so that

Code: Select all

 $this- > LoggedUser 
is assigned a

Code: Select all

 NULL 
worth and the item is freed after manufacturer returns?

Code: Select all

 $this- > LoggedUser = NULL;
if ($ _ SESSION [' verbiste_user']= false).
$ this- > LoggedUser = brand-new User($ _ SESSION [' verbiste_user']; 

User avatar
pam
Posts: 1
Joined: Sun Feb 04, 2018 12:34 pm

Re: PHP constructor to return a NULL

Post by pam » Sun Feb 25, 2018 6:33 pm

Assuming you're utilizing PHP 5, you can throw an exception in the manufacturer:

Code: Select all

 class NotFoundException extends Exception 

class User 

$ this- > LoggedUser = NULL;.
if ($ _ SESSION [' verbiste_user']= false) 
For clearness, you might wrap this in a fixed factory technique:

Code: Select all

 class User class body here ...

$ this- > LoggedUser = NULL;.
if ($ _ SESSION [' verbiste_user']= incorrect).
$ this- > LoggedUser = User:: load($ _ SESSION [' verbiste_user']; 
As an aside, some versions of PHP 4 allowed you to set $this to NULL inside the fitter but I don't believe was ever officially sanctioned and the 'feature' was ultimately removed.

Post Reply