Function overloading in CodeIgniter

I was wondering if you can overload functions in PHP, in particular in CodeIgniter. For example, in my controller, if I were to load the view, but it would be different from whether the variable was specified as a parameter or if it was left empty. This is a concept that I tried, so I learned in other languages:

<?php
  function load_view(){
     $this->load->view('view');
  }

  function load_view($var){
    $this->load->model('data');
    $data = $this->data->getInfo($var);
    $this->load->view('view', $data);
  }
?>

But when I tried this, I get the error "Fatal error: unable to override controller :: load_view" ...

Any help would be greatly appreciated. Thanks in advance!

+5
source share
1 answer

In PHP, functions use optional parameters to overload. An example would be:

function load_view($var = null) {
     if (!empty($var)) {
         $this->load->model('data');
         $data = $this->data->getInfo($var);
         $this->load->view('view', $data);
     } else {
         $this->load->view('view');
     }
}
+9
source

All Articles