0

I am trying to write a class that displays errors. I would like to be able to do it on one line. So this is what I use to display the error:

$this->error->title("Error Title")->message("This is an error message")->redirect('home')->display();

This is my class:

<?php

class Error {

    var $title;
    var $message;
    var $redirect;

    function title($title){

        $this->title = $title;

    }

    function message($message){

        $this->message = $message;

    }

    function redirect($redirect){

        $this->redirect = $redirect;

    }

    function display(){

        $CI &= get_instance();

        $CI->template->overall_header($this->title);

        $data = array(
            'error_title' => $this->title,
            'error_message' => $this->message
            );

        if(isset($this->redirect)){

            $data['redirect'] = $this->redirect;

        }

        $CI->load->view('error_body', $data);

    }

}

This is the error that I am getting:

 Fatal error: Call to a member function message() on a non-object in ...\application\frontend\controllers\members\login.php on line 128 

Why would I get the error on the message() method but not on the title method?

BlitZ
  • 12,038
  • 3
  • 49
  • 68
ShoeLace1291
  • 4,551
  • 12
  • 45
  • 81

2 Answers2

5

Method chaining requires you to put

return $this;

in the end of chainable methods.

Why would I get the error on the message() method but not on the title method?

Because your firsh chain call returns null or non-object:

$this->error->title("Error Title")->message("This is an error message")->redirect('home')->display();
//            ^^^^^^^^^^^^^^^^^^^^^^

While definition was:

function title($title){
    $this->title = $title;
} // return value not specified, so it returns null

How do you fix it? Try this:

function title($title){
    $this->title = $title;
    return $this;
}

And so on. I hope you figured out.

Community
  • 1
  • 1
BlitZ
  • 12,038
  • 3
  • 49
  • 68
1

In addition to the fact that you must enter return $this at the end of methods that are chainable, the following line is also a bit peculiar.

$this->error->title("Error Title")->message("This is an error message")->redirect('home')->display();

Since you are using $this that would mean that $this->error should be one of your properties. However you don't have a property named $error in your class. PHP doesn't really care about that, but what is does care about is that $this->error must be an instance of the Error class in order for any of your class methods to work.

I suspect that the inital reason you are getting that error message is because you are not dealing with an instance of your class. But even after you fix that you will still have to make your methods chainable to prevent more of the same message.

In this context you can use $this->title("Error Title")->message("This is an error message")..... after you make them chainable.

Also, from a design standpoint, using the var keyword is making your properties public which will make them mutable from any scope. This may be your intention, which is absolutely fine, however if your methods are going to do some extra processing leaving those as public will not enforce usage of your methods to set those properties properly.

Crackertastic
  • 4,958
  • 2
  • 30
  • 37