Jump to content

new global var


Julio Perales

Recommended Posts

Hello!

 

I need some help on this issue:

 

I'd like to have a global $week var. This week is a week class object.

 

i've created it within my ClassController, then i initialize it as global in a php document called by ajax.

I need to recover this information in other php document called by another ajax.

 

 

Controller:

 

$smarty->assign("week", "");

 

 

Module:

 

global $week;

 

Phpdoc1:

global $week;

$week = new Week(.....)

 

 

Phpdoc2:

global $week ;

 

print_r($week);

 

 

This last print_r prints "null" and i can't recover the info i need,

 

Anyone may help me? Thanks

Link to comment
Share on other sites

'global' is used to specify the lifetime of a variable and make difference between variables.

 

e.g. in your main file you have a $my_var , if you want to use it inside a function() you will need to note $my_var inside the function as global. if you do not specify $my_var as global it will be regarded as new variable, which will no longer exist after the function ends.

 

<?php
$my_var = 5;
function iAmGlobal()
{
global $my_var;
echo("iAmGlobal:".$my_var. "<br/>");
}
function iAmLocal()
{
$my_var;
echo("iAmLocal:".$my_var. "<br/>");
}
iAmGlobal();
iAmLocal();
?>

 

First function will output 5, the second one will output NULL , because the local $my_var didn't receive an initial value.

 

With your example $week would be accessible from PHPdoc2 if PHPdoc1 would be included in it.

 

Use cookies.

Link to comment
Share on other sites

Thanks very much!

 

I've done global request in my methods, maybe there's no the problem..

 

First of all, i've tried to do it using cookies, but i've a wide complex object and with this data cookies don't make easier the work, in this case i've to serialize and unserialize a huge quantity of data (complex objects)

 

So i've decided to do it via database, either i'd like to find out if there would be a solution based on global vars.

Link to comment
Share on other sites

If this is a variable you need to use in a lot of/ all the files, put it in a file which is included everywhere.

 

That's what i was trying..

Prestashop makes global $cookie, and other in class FrontController..

I extend Frontcontroller and make global var in my own controller, but then i can´t use the variable in differentes scripts of the same module.. I don't understand..

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...