0

I'm trying to create custom exception class for codeigniter. Here is my code:

class Ci_exception extends Exception {

    var $CI;

    function __construct()
    {
        $this->CI =& get_instance();
    }

    public function errorMessage()
    {
        log_message('error', $this->getMessage());
        return $this->getMessage();
    }

}

This custom class works fine if there is no constructor method. With the construct method the $this->getMessage() method doesn't return any message. Can anyone explain why?

1 Answer 1

1

You need to call the parent's constructor; it's not implicit in PHP. You'll also probably want to accept a $message parameter.

class Ci_exception extends Exception {

    var $CI;

    function __construct($message)
    {
        parent::__construct($message);
        $this->CI =& get_instance();
    }

    public function errorMessage()
    {
        log_message('error', $this->getMessage());
        return $this->getMessage();
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.