King Julien
King Julien

Reputation: 11308

Create custom exception class for codeigniter

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?

Upvotes: 0

Views: 1824

Answers (1)

Ry-
Ry-

Reputation: 224942

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();
    }

}

Upvotes: 1

Related Questions