Custom controller won't inherit variables - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Custom controller won't inherit variables (/showthread.php?tid=54323) |
Custom controller won't inherit variables - El Forum - 09-03-2012 [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.'); Code: <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); Code: <!DOCTYPE html> Custom controller won't inherit variables - El Forum - 09-03-2012 [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"; Custom controller won't inherit variables - El Forum - 09-03-2012 [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"; 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: <?php if (! defined('BASEPATH')) exit('No direct script access allowed.'); It says that the property is undefined, so I'm not quite sure what's up? Custom controller won't inherit variables - El Forum - 09-04-2012 [eluser]Rolly1971[/eluser] it is like this: Code: $this->loggedin = 'test'; not: Code: $this->$loggedin = 'test'; see the difference? Custom controller won't inherit variables - El Forum - 09-04-2012 [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. |