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:
$this->func = is_callable(array($this->db, '_compile_select')) ? '_compile_select' : 'get_compiled_select';
if ( ! is_callable(array($this, $this->func)))
{
show_error("You can't use this library, you're missing a function");
}
Basically, just don't assume get_compiled_select is callable in your conditional - check first.
can I call show_error from a library?
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 to catch errors and try something else:
try {
$this->load->library('might_not_exist', 'alias');
} catch (Exception $e) {
$this->load->library('definitely_exists', 'alias');
}
The above won't work, as show_error() will get called by the loader and exit the program before your catch block is executed.
I am not sure but see this URL i think it is very help full to you.
http://codeigniter.com/forums/viewthread/67096/
see also
http://phpcodeignitor.blogspot.in/2011/07/php-exception.html
or try this
MY_Exceptions.php and put it in /applications/libraries/
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions{
function MY_Exceptions(){
parent::CI_Exceptions();
}
function show_404($page = '')
{
echo 'test';
}
function show_error($heading, $message, $template = 'error_general')
{
echo 'test';
}
function show_php_error($severity, $message, $filepath, $line)
{
echo 'test';
}
}
?>
functions in MY_Exceptions do not seem to be overridden at all. functions in Exceptions are the one which are run
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With