• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Custom controller won't inherit variables

#1
[eluser]Madigan[/eluser]
Hey, I'm trying to learn codeigniter and make a simple blog. In order to have pages that react to different types of users (not logged in, logged in, admin, etc.), I am extending the base controller so that it has a little "logged in" variable. However, when I extend my controller, I get an error saying that the variable doesn't exist when I try to extend the class.

Code:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed.');

class MY_Controller extends CI_Controller
{
public $loggedIn = "";
function __construct()
{
  parent::__construct();
  //$loggedIn = $this->session->userdata('privileges');
  $loggedIn = "test";
}
}

?>
Code:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class CHome extends MY_Controller
{
function __construct()
{
  parent::__construct();
}

function index()
{
  $this->load->view('vhome', array('loggedIn' => $loggedIn));
}
}

?>
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;My Site&lt;/title&gt;[removed]
&lt;/head&gt;
&lt;body&gt;
  &lt;header id="header"&gt;&lt;/header>
  <nav id="navBar"></nav>
  <section id="content">&lt;? echo $loggedIn; ?&gt;</section>
  <footer id="footer"></footer>
&lt;/body&gt;
&lt;/html&gt;
I think I should be able to access any public/protected variables from the parent class. However, for some reason it doesn't seem to be working. Any ideas?

#2
[eluser]CroNiX[/eluser]
Because in your construct, you are setting a local variable, not a class property. You need to reference the class using $this to set class properties...

Code:
$this->loggedIn = "test";

#3
[eluser]Madigan[/eluser]
[quote author="CroNiX" date="1346717003"]Because in your construct, you are setting a local variable, not a class property. You need to reference the class using $this to set class properties...

Code:
$this->loggedIn = "test";
[/quote]

I'm sorry, I'm more used to Java/C# so I'm a little slow at this...
When I make that change, I'm still getting a scope errors on line 10:
Code:
&lt;?php if (! defined('BASEPATH')) exit('No direct script access allowed.');

class MY_Controller extends CI_Controller
{
protected $loggedIn;
function __construct()
{
  parent::__construct();
  //$loggedIn = $this->session->userdata('privileges');
  $this->$loggedIn = "test";
}
}

?&gt;

It says that the property is undefined, so I'm not quite sure what's up?

#4
[eluser]Rolly1971[/eluser]
it is like this:

Code:
$this->loggedin = 'test';

not:

Code:
$this->$loggedin = 'test';

see the difference?

#5
[eluser]Madigan[/eluser]
[quote author="Rolly1971" date="1346770330"]it is like this:

Code:
$this->loggedin = 'test';

not:

Code:
$this->$loggedin = 'test';

see the difference?[/quote]

*facepalm*

Yeah, thanks.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.