Php Error In Constructor
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site php constructor return false About Us Learn more about Stack Overflow the company Business Learn more about
Php Throw Exception
hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join php __construct the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up PHP constructor to return a NULL up vote 41 down vote favorite 17 I have this code. Is it possible for a User object constructor to somehow fail so that $this->LoggedUser is assigned a NULL value and the object is freed after constructor returns? $this->LoggedUser = NULL; if ($_SESSION['verbiste_user'] != false) $this->LoggedUser = new User($_SESSION['verbiste_user']); php constructor error-handling null share|improve this question edited Sep 29 '11 at 20:19 Trott 28.1k1471129 asked Feb 6 '10 at 21:03 Tibor 4,14341464 7 Great first question by the way. –Pekka 웃 Feb 6 '10 at 21:20 I've seen a certain popular CMS that returns FALSE in a constructor. What's up with that?!?! –loungerdork Oct 31 '11 at 3:17 1 Just thought I'd chime in here for documentation sake. Since the date is so far back, it is possible that the CMS you are seeing was built for PHP4. PHP4 used to allow a lot of bad things, not the least of which allowed the user to overwrite $this in the named constructor (for example $this = false). –techdude Dec 31 '14 at 22:52 add a comment| 6 Answers 6 active oldest votes up vote 54 down vote accepted Assuming you're 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 could wrap this in a static factory m
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Is it possible to exit gracefully in a constructor? up vote 5 down vote favorite 1 Is it possible to exit gracefully out of a constructor in php? Something to the effect of class Foo { http://stackoverflow.com/questions/2214724/php-constructor-to-return-a-null function __construct() { $active = false; if(!$active) { return false; } } } I'm trying to accomplish this because I want to check to see if any of the methods in the class should run based on a configuration file. I don't want to have to check the configuration file in every method to see if the methods should be running. php constructor return-value share|improve this question asked May 29 '09 at 14:27 Scott add a comment| 6 Answers 6 active http://stackoverflow.com/questions/926269/is-it-possible-to-exit-gracefully-in-a-constructor oldest votes up vote 7 down vote That depends on what you mean by "gracefully". If you want your constructor to fail you can throw an exception, or you can use the factory pattern: class FooFactory { function makeFoo() { return $someConstraint ? null : new Foo(); } } Maybe you can elaborate a bit on what exactly it is you want to accomplish. share|improve this answer answered May 29 '09 at 14:34 n3rd 3,88312451 1 I'm calling this class from another file. If I set $active to be false, I would want none of the methods to run, and for the other page to load normally. For example, if I put a die; in the construct, it will die for every page that has called that object. –Scott May 29 '09 at 15:09 In that case you could use the factory pattern to return a dummy object if $active is false and the "real" object if it is not. This isn't the most elegant solution but it would do the trick nicely. –n3rd May 29 '09 at 15:24 add a comment| up vote 3 down vote Make it blow up. There is no such thing as a graceful failure in a constructor. Perhaps in the calling code, but not in the constructor. Throw an exception and handle it appropriately. share|improve this answer answered May 29 '09 at 15:14 Brantley add a comment| up vote 3 down vote
and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers http://php.net/manual/en/exception.construct.php Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting Using Register Globals User Submitted Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent php error Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Credit Card Processing Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image php error in Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box Exception::getMessage » « Exception PHP Manual Language Reference Predefined Exceptions Exception Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug Exception::__construct (PHP 5 >= 5.1.0, PHP 7)Exception::__construct — Construct the exception Description public Exception::__construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] ) Constructs the Exception. Parameters message The Exception message to throw.