• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Centralizing user-defined error.


I'm building software using of course CI and I want to make my user-defined errors centralized. I tried creating class extending from CI_Model and that class is base class of my other classes. That base class extending from CI_Model is located in the core dir. Though it works very well but I'm worried it's not a best practice? If not what can you suggest instead? Please give me insights. Here's my sample code.

class baseClass2 extends CI_Model {

const ERRORSTR_EMPTY_VALUE = "Data should not be empty string.";

const ERRORSTR_INVALID_PARAMETER = "Invalid passed parameter.";

const ERRORSTR_DB_VALUE_EXISTS = "Database value already exist.";

public function __construct() {

protected function checkArrayKeyExists(array $needles, array $haystack) {
foreach ($needles as $needle) {
if ( ! array_key_exists($needle, $haystack)) {
error_log(self::ERRORNO_INVALID_PARAMETER . ": " . self::ERRORSTR_INVALID_PARAMETER . " '$needle' ");
return false;

return true;

and in my controller dir

class Company_model extends baseClass2 {



The strings for the error messages should really be loaded from a lang file so they can be localized, if necessary, or, at least, be available in one location which can be easily updated by people with a fairly limited knowledge of PHP code.

Generally, I would use MY_Model (or whatever prefix is defined for your application) for the base class name, because people familiar with CodeIgniter will tend to look there, first, for your base model, but I've certainly had situations in which I've needed to use other base models. It works out fine as long as people are careful in their troubleshooting/debugging and you don't duplicate functionality in multiple base models.

I would also avoid names like baseClass2 in favor of something more descriptive, but I'm hoping that was just an example. It may be easy in the short term to have baseClass, baseClass2, baseClass3, but, in the long run, or when someone else has to deal with the code, someone will have to look at the code to figure out what the difference is between the classes, where a better name might prevent some confusion.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.