Php Error Handling In Class
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 About Us Learn more about Stack Overflow the company Business Learn more php error handling best practices about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users
Php Error Handling Tutorial
Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping php exception class each other. Join them; it only takes a minute: Sign up Error Handling in a PHP Class up vote 2 down vote favorite 3 Hey there, here is a question for you guys. I have so much times
Php Error Reporting
to choose a error handling for classes in PHP. For Example in Ajax PHP Handling Classes i do it this way: public function setError($msg) { $this->errors[] = $msg; } public function isFailed() { return (count($errors) > 0 ? true : false); // if errors > 0 the request is failed } public function getJsonResp() { if($this->isFailed()) { $resp = array('result' => false, 'message' => $this->errors[0]); } else { $resp = array('result' => true); array_merge($resp, $this->success_data); // the php try catch example success data is set later } return json_encode($resp); } // an example function for a execution of a method would be this public function switchMethod($method) { switch($method) { case 'create': if(!isset($param1, $param2)) { $this->setError('param1 or param2 not found'); } else { $this->createSomething(); } break; default: $this->setError('Method not found'); } } So lets know you what i want to aks for: Is there a better solution for error handling? php class error-handling share|improve this question edited Jun 5 '14 at 8:52 Raidri 11.3k62142 asked Feb 13 '11 at 19:53 000 252211 add a comment| 2 Answers 2 active oldest votes up vote 8 down vote accepted When it comes to OOP Your best bet is to use Exceptions to handle your errors, for example: class Example extends BaseExample implements IExample { public function getExamples() { if($this->ExamplesReady === false) { throw new ExampleException("Examples are not ready."); } } } class ExampleException extends Exception{} throwing exceptions within your class and catching exceptions outside of the classes that throw them is the way I usually go about things. Usage Example: $Example = new Example(); try { $Examples = $Example->getExamples(); foreach($Examples as $Example) { //... } }catch(ExampleException $e) { Registry::get("Output")->displayError("Unable to perform action",$e); } and your displayError would use $e->getMessage() as the information regarding the error. share|improve this answer edited Apr 22 '15 at 22:01 James 33 answered Feb 13 '11 at 20:07 RobertPitt
and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem
Php Exception List
Security Database Security Error Reporting Using Register Globals User Submitted Data Magic Quotes Hiding
Php Exception Types
PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling php custom exception Persistent 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 http://stackoverflow.com/questions/4986476/error-handling-in-a-php-class Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image 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 http://php.net/manual/en/language.exceptions.php 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 Extending Exceptions » « Errors in PHP 7 PHP Manual Language Reference Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug Exceptions Table of ContentsExtending Exceptions PHP 5 has an exception model similar to that of other programming languages. An exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block, to facilitate the catching of potential exceptions. Each try must have at least one corresponding catch or finally block. The thrown object must be an instance of the Exception class or a subclass of Exception. Trying to throw an object that is not will result in a PHP Fatal Error. catch Multiple catch blocks can be used to catch different classes of exceptions. Normal execution (when no exception is thrown within the try block) will continue after that last catch block defined in sequence. Exceptions can be thrown (or re-thrown) within a catch block. When an exception is thrown, code following the
and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module http://php.net/manual/en/class.exception.php Session Security Filesystem Security Database Security Error Reporting Using Register Globals User Submitted http://php.net/manual/en/function.set-exception-handler.php 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 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 php error 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 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 php error handling 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::__construct » « Predefined Exceptions PHP Manual Language Reference Predefined Exceptions Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug Exception (PHP 5 >= 5.1.0, PHP 7) Introduction Exception is the base class for all Exceptions in PHP 5, and the base class for all user exceptions in PHP 7. In PHP 7, Exception implements the Throwable interface. Class synopsis Exception { /* Properties */ protected string $message ; protected int $code ; protected string $file ; protected int $line ; /* Methods */ public __construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] ) final public string getMessage ( void ) final public Exception getPrevious ( void ) final public mixed getCode ( void ) final public string getFile ( void ) final public int getLine ( void ) final public array
and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers 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 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 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 trigger_error » « set_error_handler PHP Manual Function Reference Affecting PHP's Behaviour Error Handling Error Handling Functions Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug set_exception_handler (PHP 5, PHP 7)set_exception_handler — Sets a user-defined exception handler function Description callable set_exception_handler ( callable $exception_handler ) Sets the default exception ha