Welcome Guest, Not a member yet? Register   Sign In
How to call a variable globally
#1

[eluser]cinewbie81[/eluser]
Hi guys,

I have a file ‘globalsetting.php’ as following:

Code:
<?php

class Globalsetting extends Controller {

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

    // Initialization function  
    function Initialize()
    {
           $DB1 = $this->load->database('MYDB1', TRUE);
           $DB2 = $this->load->database('myDB2', TRUE);
           $MyClient = "DELL";            
    }
}
My problem is:

How can i call variables DB1, DB2, and MyClient from other controller class ?? Anyone ? IS that correct that I put Globalsetting class extend from controller ? Hope someone can helkp as I’m stuck here for quite a while .. thanks in advance
#2

[eluser]Colin Williams[/eluser]
Controllers handle your application logic and are typically mapped to a specific URL. Models interact with the database so your controller doesn't have to. Then, views handle the presentation.

By your code, the 'Initialize' method is mapped to mysite.com/globalsetting/Initialize. CI provides config files, Libraries, Helpers, and Models for interaction on a global scope. please read through the first part of the user guide to learn more about CI's approach.
#3

[eluser]thurting[/eluser]
I don't really know what you are trying to accomplish with that class, but it doesn't look like it should be a subclass of Controller. In any case, if you want to define a global constant you should check out the define() function which is native to PHP. You could also define your global variables as static properties of a class. I would suggest coding in PHP5 if you were going to go this route. For example, you could create a class (GlobalSettings) with a static property (db) and access it via GlobalSettings::db. Depending on your application, you may want to make that class a singleton. You could also store your variables in SESSION, but you probably wouldn't want to do that.
#4

[eluser]cinewbie81[/eluser]
[quote author="thurting" date="1193050783"]I don't really know what you are trying to accomplish with that class, but it doesn't look like it should be a subclass of Controller. In any case, if you want to define a global constant you should check out the define() function which is native to PHP. You could also define your global variables as static properties of a class. I would suggest coding in PHP5 if you were going to go this route. For example, you could create a class (GlobalSettings) with a static property (db) and access it via GlobalSettings::db. Depending on your application, you may want to make that class a singleton. You could also store your variables in SESSION, but you probably wouldn't want to do that.[/quote]

I hope i got what u mean ..
DO u mean create a class call GlobalSettings without extend from anything (not controller, model etc) like following:

Class GlobalSettings()
{
// Declare my variable here
}

Where should i put this file ? Under controller folder ?
And how can i call it from other controller class ?
Sorry I'm quite new to php Sad
Some sample code will be appreciated thx
#5

[eluser]sharpe[/eluser]
Try to read more about hooks. I thing this is what you want.
#6

[eluser]Michael Wales[/eluser]
For global vars - I would just use a custom configuration file.
#7

[eluser]cinewbie81[/eluser]
[quote author="walesmd" date="1193053620"]For global vars - I would just use a custom configuration file.[/quote]


hi, cheers ..
but what if the value of that variable is actually get from the database ?
#8

[eluser]Michael Wales[/eluser]
You could do it with hooks, but those are hard for others to understand as there isn't a prevalent code trail to follow.

Maybe create a custom controller and then have all of your app controllers extend the custom controller.
#9

[eluser]thurting[/eluser]
If you stick you custom class in a folder called application/libraries you can load/instantiate the class using CI's built in load method. You could also just include a require statement at the top of the script using the class and then you could place it anywhere.
#10

[eluser]cinewbie81[/eluser]
thx walesmd, thurting, sharpe and colin .. it's now SOLVED Smile




Theme © iAndrew 2016 - Forum software by © MyBB