I’ve made a CodeIgniter library that depends on a certain method existing. This method used to be “hidden” (it was not documented, but seemed to work). Eventually CodeIgniter made it protected, so I can’t call it from a library. In the dev version of CodeIgniter on GitHub, there is a new public method that I can use.
In my library, I use is_callable to detect which method to use, the old method or the new one. Problem is, in the current stable version of CodeIgniter, neither exist. Because of this, the library will fail. Is there a way I can gracefully error out, or throw an exception from within my constructor? Currently, if neither method is available, the script will just crash when it tries to call the method.
I don’t know what the convention is for a CodeIgniter library not being able to load correctly because a method is missing.
EDIT: Here is the line I am asking about:
$this->func = is_callable(array($this->db, '_compile_select')) ? '_compile_select' : 'get_compiled_select';
If neither of these exist (_compile_select or get_compiled_select), then it will error out when the library tries to call $this->func. I don’t know the convention , can I call show_error from a library? What’s the correct way to throw an error from a library’s constructor?
If the goal is just a pretty error message instead of a fatal error, it’s pretty simple really:
Basically, just don’t assume
get_compiled_selectis callable in your conditional – check first.Yes, this is one of the functions defined in
core/Common.php, you can use it anywhere in your CI app.Of course, technically this is not
throwing an exception, but it’s the convention for Codeigniter. This can be problematic if you want tocatcherrors and try something else:The above won’t work, as
show_error()will get called by the loader and exit the program before your catch block is executed.