Welcome Guest, Not a member yet? Register   Sign In
Can't get the variable value from extended class
#1

[eluser]cinewbie81[/eluser]
Hi all,

I have 3 controller class files as following:

globalclass.php
Code:
class Globalclass extends Controller {
    
    var $loginName

    function Globalclass()
    {    
        parent::Controller();

    }

    function setLoginName($myloginname)
    {
        $this->loginname = $myloginname;
    }
}

login.php - Extend Globalclass
Code:
class Login extends Globalclass {

    function Login()
    {    
        parent::Globalsetting();
        $this->load->helper(array('url','form'));

    }

    function index()
    {
        $this->load->view('form_login', null);        
    }

    function afterlogin()
    {
        $username = $this->input->post('user_name');
        $this->setLoginName($username);    
        redirect('home');
    }

}

home.php - Extend Globalclass
Code:
class Home extends Globalclass {

    function Home()
    {
        // load library and helper files
        parent::Controller();
        $this->load->helper(array('url','form' ));
    }
    
    function index()
    {
        echo $this->loginname;
    }
}


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 ??
#2

[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.
#3

[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.
#4

[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 ??
#5

[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 Sad
#6

[eluser]alexsancho[/eluser]
Why not use an include in your extended classes?

Code:
include('path/globalclass.php');

class Home extends Globalclass {

    function Home()
    {
        // load library and helper files
        parent::Controller();
        $this->load->helper(array('url','form' ));
    }
    
    function index()
    {
        echo $this->loginname;
    }
}
#7

[eluser]cinewbie81[/eluser]
[quote author="alexsancho" date="1193490597"]Why not use an include in your extended classes?

Code:
include('path/globalclass.php');

class Home extends Globalclass {

    function Home()
    {
        // load library and helper files
        parent::Controller();
        $this->load->helper(array('url','form' ));
    }
    
    function index()
    {
        echo $this->loginname;
    }
}
[/quote]

I did put an include there, just didnt post the code here .. but still it wont solve the problem Sad
#8

[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
#9

[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) .
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB