scott212 Posted January 16, 2007 Share Posted January 16, 2007 I've had a few rounds with some classes and I'm loving it. But I've gotten to the point where I've got some theory questions. When I purposely make a mistake inside one of the functions below, the error doesn't report if the die statement is on the function call like below. How would err handling usually be handled inside of a class? Also, does anyone see anything I'm doing majorly wrong or innefficient?Here's the class structure that sparked it:[code]<?phpclass do_stuff { var error = 0; var message;function get_input ($input_1, $input_2) { $this->var_1 = $input_1; $this->var_2 = $input_2;}function method_1() { do stuff here;}function method_2() { do stuff here;}function err_handler($message) { $this->error = 1; $this->message = $message;}function main() { $this->method_1() or die(err_handler("error on method_1")); $this->method_2() or die(err_handler("error on method_2")); $output = array($this->error, $this->message); return $output}}?>[/code]and of course the call:[code]<?php$doClass = new do_stuff;$doClass->get_input($a, $b);$return = $doClass->main;echo $return[0]."<br>".$return[1];?>[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted January 16, 2007 Share Posted January 16, 2007 IMO errors should allways be handled by the client code, not by the object itself. In php4 your methods should return true / false, and If you like you could also create an error() method which might display the last error. In php5 you should throw exceptions and have your client code handle them. Quote Link to comment Share on other sites More sharing options...
scott212 Posted January 16, 2007 Author Share Posted January 16, 2007 I just noticed I missed the $this-> preceding the err_handler calls Quote Link to comment Share on other sites More sharing options...
scott212 Posted January 16, 2007 Author Share Posted January 16, 2007 thanks for your reply Thorpe - and pardon the newbieism, but could you give any kind of example? Quote Link to comment Share on other sites More sharing options...
play_ Posted January 16, 2007 Share Posted January 16, 2007 Example of what? methods returning true/false, creating an error method, or exceptions? Quote Link to comment Share on other sites More sharing options...
scott212 Posted January 16, 2007 Author Share Posted January 16, 2007 [quote]In php5 you should throw exceptions and have your client code handle them.[/quote]not sure what he means by 'throw exceptions' Quote Link to comment Share on other sites More sharing options...
play_ Posted January 16, 2007 Share Posted January 16, 2007 [quote author=scott212 link=topic=122590.msg505848#msg505848 date=1168926278][quote]In php5 you should throw exceptions and have your client code handle them.[/quote]not sure what he means by 'throw exceptions'[/quote][url=http://us2.php.net/manual/en/language.exceptions.php]http://us2.php.net/manual/en/language.exceptions.php[/url] :)useful stuff Quote Link to comment Share on other sites More sharing options...
scott212 Posted January 16, 2007 Author Share Posted January 16, 2007 excellent information! thank you all very much Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.