Welcome Guest, Not a member yet? Register   Sign In
Can't access parent class' variables
#1

[eluser]Nuu[/eluser]
I was trying to stick some header data into MY_Controller class then access it from all the other pages. I keep getting the error 'Undefined Variable' when I try to access the variable ['mytitle'] in the view.

This is my controller placed in Application/library/ folder.

Code:
<?php

class MY_Controller extends Controller {

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

class Arc_Controller extends MY_Controller {

    var $data = array();

    function __contruct()
    {
        parent::MY_Controller();
        $this->data['mytitle'] = 'My Title';
    }
}
?>

This is my test controller.

Code:
<?php

class Test extends Arc_Controller {

    function __contruct()
    {
        parent::Arc_Controller();
    }
    
    function index()
    {
        $data['heading'] = 'My heading';
        $this->load->view('test_view', $data);
    }
}

?>

Here's my test view.

Code:
<html>
<head>
<title><?$mytitle?></title>
</head>
<body>

<h1>&lt;?=$mytitle?&gt;</h1>
<h1>&lt;?=$heading?&gt;</h1>

&lt;/body&gt;
&lt;/html&gt;


I have been looking at it for the last hour and just can't see why its wrong. I see a number of threads making reference to this method of coding e.g.



How to extend controller class

http://ellislab.com/forums/viewthread/71468/

Help ... :gulp:
#2

[eluser]Armchair Samurai[/eluser]
In your view, try_calling $this->data['mytitle'], not $mytitle or adding $data['mytitle'] = $this->data['mytitle'] to your array in test()

Also (I'm assuming this is a typo), you forgot to echo a value between the &lt;title&gt; tags.
#3

[eluser]Nuu[/eluser]
I tried the view like this:

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?$mytitle?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<h1>&lt;?=$mytitle?&gt;</h1>
<h1>&lt;?=$heading?&gt;</h1>
<h1>&lt;? echo $this->data['mytitle']; ?&gt;</h1>

&lt;/body&gt;
&lt;/html&gt;

I get the error "undefined index".
#4

[eluser]Colin Williams[/eluser]
You need to pass the $this->data to your view, not $data.




Theme © iAndrew 2016 - Forum software by © MyBB