Welcome Guest, Not a member yet? Register   Sign In
Function which is run at the end
#11

[eluser]juan1904[/eluser]
Controller, main.php:
Code:
class Main extends MY_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $data['content'] = 'main';
    }
}

Controller, MY_Controller.php:
Code:
class MY_Controller extends Controller {

    function __construct()
    {
        parent::Controller();
    }

    function __destruct()
    {
        $this->load->vars($this->data);
    }
}

Gives this error on my view-page:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: content

Filename: libraries/Loader.php(673) : eval()'d code

Line Number: 7


An Error Was Encountered

Unable to load the requested file: .php
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Main::$data

Filename: libraries/MY_controller.php

Line Number: 12
#12

[eluser]Dam1an[/eluser]
Just a few tweaks, try
Code:
// Main.php
class Main extends MY_Controller {
    function __construct() {
        parent::__construct();
    }

    function index() {
        // You need $this so it uses the data array in the parent class
        $this->data['content'] = 'main';
    }
}

// MY_Controller
class MY_Controller extends Controller {
    // You never declared this
    // TO make it PHP4 compatible, delare it as a var
    protected $data = array();
    
    function __construct() {
        parent::Controller();
    }

    function __destruct() {
        $this->load->vars($this->data);
    }
}
#13

[eluser]juan1904[/eluser]
Alright. Then I do find it better to put "$this->load->vars($this->data);" in the end of each controller anyway.

Why doesn't I need $this->data if I just extend Controller?
#14

[eluser]Dam1an[/eluser]
Because when you extend the CI controller, there is no data array, you create one local to the method, but you needed to have it in the scope of the class before so you could load it from MY_Controller




Theme © iAndrew 2016 - Forum software by © MyBB