0

I read this question but it can't help me a lot...

Using Ion_auth library I noted that the data to a view are passed by $this->data[] and not simple by $data.

Why? Where is the difference or the vantage to do this?

Community
  • 1
  • 1
Fra Ore
  • 59
  • 2
  • 6

2 Answers2

1

Using $this->data means you can make use of OOP inheritance of a parent controller. The parent controller such as MY_controller can set some initial data which becomes available in all controllers that extend it.

There's an example here: http://avenir.ro/codeigniter-tutorials/creating-working-with-my_controller-codeigniter/

Zbitset
  • 11
  • 1
0

$this->data[] That means you have to define the $data member in your class,

And $this reference the current object, simply the current class

When you define

Class Oop
{
    private $data = array();

    function test()
    {
        $this->data['title'] = 'Home';
        echo $this->data['title']; //access like this
    }
}

In codeigniter $data is an array

In controller you can $data['title'] = 'Home'; Then in view you can access it like $title;

Please reference this oop

jlocker
  • 1,478
  • 1
  • 12
  • 23
  • Ok Thanks Jlocke, but I want to know the reals vantages. For example: In the controller Auth.php (ion_auth library) at line 81 there is: $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message'); and at line 93: $this->_render_page('auth/login', $this->data); But if I change $data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message'); and $this->_render_page('auth/login', $data); the result is exactly the same. So why use $this when is not necessary? – Fra Ore Oct 04 '15 at 04:09
  • @FraOre check the _render_page() function and library then you will realize – jlocker Oct 04 '15 at 04:30