EllisLab text mark
Advanced Search
Method chaining and errors
Posted: 20 July 2010 05:09 AM
Joined: 2008-10-20
398 posts

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.


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?

if (
$lib->error) print $lib->display_errors(); 

Is that the correct way to handle it? I don’t want to just kill the script!


Image moo - the easy to use image library!
E-Mail Remind - Free reminders by email
Printfetti - Your photos on confetti!

Posted: 20 July 2010 11:38 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-10-18
334 posts

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:

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.


Wallpapers and Images Site: Desktop Wallpapers