![]() |
Can't get the variable value from extended class - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Can't get the variable value from extended class (/showthread.php?tid=3883) |
Can't get the variable value from extended class - El Forum - 10-27-2007 [eluser]cinewbie81[/eluser] Hi all, I have 3 controller class files as following: globalclass.php Code: class Globalclass extends Controller { login.php - Extend Globalclass Code: class Login extends Globalclass { home.php - Extend Globalclass Code: class Home extends Globalclass { In afterlogin() function for Login.php class, I call this statement "$this->setLoginName($username)", which will set variable $loginName in Class Globalclass.php to the value i passes. Then i redirect the system to Home and echo $this->loginname value, it return me nothing. Why is that so anyone ?? Can't get the variable value from extended class - El Forum - 10-27-2007 [eluser]xwero[/eluser] Probably because you make new instances on the different pages. Try autoloading the global class and calling the extended classes when you need them. Can't get the variable value from extended class - El Forum - 10-27-2007 [eluser]Rick Jolly[/eluser] It is because you are redirecting. That sends a response to the client browser to make a new request to the Home controller. You lose state. You would need to put the login name into the session. Can't get the variable value from extended class - El Forum - 10-27-2007 [eluser]cinewbie81[/eluser] Hi, This is only one variable .. Probably I'll have 100 variables in that class ... it's impossible for me to save all in the sessions right ?? Can't get the variable value from extended class - El Forum - 10-27-2007 [eluser]cinewbie81[/eluser] [quote author="xwero" date="1193485272"]Probably because you make new instances on the different pages. Try autoloading the global class and calling the extended classes when you need them.[/quote] Hi, In autoload.php it said: | ------------------------------------------------------------------- | Instructions | ------------------------------------------------------------------- | | These are the things you can load automatically: | | 1. Libraries | 2. Helper files | 3. Plugins | 4. Custom config files | 5. Language files My globalsetting.php is extended from controller .. so i cant set it to auload ![]() Can't get the variable value from extended class - El Forum - 10-27-2007 [eluser]alexsancho[/eluser] Why not use an include in your extended classes? Code: include('path/globalclass.php'); Can't get the variable value from extended class - El Forum - 10-27-2007 [eluser]cinewbie81[/eluser] [quote author="alexsancho" date="1193490597"]Why not use an include in your extended classes? Code: include('path/globalclass.php'); I did put an include there, just didnt post the code here .. but still it wont solve the problem ![]() Can't get the variable value from extended class - El Forum - 10-27-2007 [eluser]xwero[/eluser] [quote author="cinewbie81" date="1193489234"]Hi, This is only one variable .. Probably I'll have 100 variables in that class ... it's impossible for me to save all in the sessions right ??[/quote] What needs 100 variables to be stored in the memory? I think if you want do do something like that you should make a table where you store those variables and call them using the loginname/id Can't get the variable value from extended class - El Forum - 10-27-2007 [eluser]cinewbie81[/eluser] Hi, I'm just giving an example for 100 variable thingy .. Probably not 100 variable, but maybe 6, 7 or something .. I'm just wondering besides store it in session, is there any other way for me to using the code i post above (with some modification of course) . Can't get the variable value from extended class - El Forum - 10-27-2007 [eluser]Paul Scott[/eluser] This is happening because PHP variables are only kept per request. If you want to keep a variable between requests, then you can store the value in a cookie (provided it is a simple data type; eg. string or number) or in a session. Therefore, when you set the variable in your `Globalclass`, the value is set as you would expect only for you to redirect the user to another page (the user makes a new request for the home page) and this time PHP does not set again the value for the login name. Perhaps you are misunderstanding what the `redirect` function does, but it works the same as using `header('Location: ...');` and so creates a seperate request. |