Welcome Guest, Not a member yet? Register   Sign In
Method chaining and errors
#1

[eluser]Mat-Moo[/eluser]
I'm working on a small image library and I'm playing with method chaining. This is my first time of writing method chaining and I understand the principal, but not sure on how to handle errors.
Code:
$lib->load_image("x")->resize(200,200);
Pretty simple, load_image should return $this, but what if the file was not found? Do I just set my error internally and you check after?
Code:
$lib->load_image("x")->resize(200,200);
if ($lib->error) print $lib->display_errors();
Is that the correct way to handle it? I don't want to just kill the script!
#2

[eluser]Buso[/eluser]
I usually make these kind of methods return something (or TRUE), the image maybe in this case, or FALSE on failure.
Then I can do this:
Code:
if( ! $new_img = $lib->resize()) {
  echo $lib->errors();
}

For more critical errors I just throw exceptions with an exception thrower (a method that helps me throw different kinds of exceptions) and try to catch them, or let a custom exception handler do it.




Theme © iAndrew 2016 - Forum software by © MyBB