Page 1 of 1
Global variables in function
Posted: Tue Jun 10, 2008 3:31 pm
by Sonya
Hello,
I have understanding problem with global variables. I try to execute CMSMS Code externally. It works this way:
Code: Select all
<?
include_once($_SERVER['DOCUMENT_ROOT'].'/include.php');
$news= $gCms->modules["News"]["object"];
?>
But if I call the same code within function global variable $config is not available and code breaks:
Code: Select all
<?
function global_test() {
include_once($_SERVER['DOCUMENT_ROOT'].'/include.php');
$news= $gCms->modules["News"]["object"];
}
global_test();
?>
Why is global $config not available in the last code?
Thank you for your help,
Sonya
Re: Global variables in function
Posted: Tue Jun 10, 2008 3:41 pm
by Wiedmann
Why is global $config not available in the last code?
$gCms is a variable in the global scope, and not a superglobal variable which is availible everywhere.
-->
http://de.php.net/manual/en/language.va ... .scope.php
You can use:
Code: Select all
global $gCms;
$news =& $gCms->modules['News']['object'];
or
Code: Select all
$news =& $GLOBALS['gCms']->modules['News']['object'];
Re: Global variables in function
Posted: Tue Jun 10, 2008 3:44 pm
by calguy1000
Why is global $config not available in the last code?
you should not be using the $config global (and we're going to phase it out of the core too), because there is too much possibility for conflicts with other external scripts that you may be using.
instead do this:
Code: Select all
global $gCms; // this name probably won't conflict
$config =& $gCms->GetConfig();
Re: Global variables in function
Posted: Tue Jun 10, 2008 5:29 pm
by Sonya
Wiedmann wrote:
$gCms is a variable in the global scope, and not a superglobal variable which is availible everywhere.
$gCms is set and available, but $config is empty.
Re: Global variables in function
Posted: Tue Jun 10, 2008 5:32 pm
by calguy1000
This worked for me.
Code: Select all
<?php
include('include.php');
global $gCms;
$config =& $gCms->GetConfig();
print_r( $config );
?>
Re: Global variables in function
Posted: Tue Jun 10, 2008 5:36 pm
by Sonya
calguy1000 wrote:
Why is global $config not available in the last code?
you should not be using the $config global (and we're going to phase it out of the core too), because there is too much possibility for conflicts with other external scripts that you may be using.
instead do this:
Code: Select all
global $gCms; // this name probably won't conflict
$config =& $gCms->GetConfig();
Thank you. But I cannot use this this way. In the external script I am not in the environment of CMSMS and therefore I have to create $gCMS first. I thought I can just include "include.php".
I have to correct myself. I have to include CMSMS code not the function but in a class method this way:
Code: Select all
class Test {
function global_test() {
global $gCms, $config;
include_once($_SERVER['DOCUMENT_ROOT'].'/include.php');
$news = $gCms->modules["News"]["object"];
}
}
$instance = new Test();
$instance->global_test();
This code breaks because $config is empty in load_adodb() function called in include.php.

$config is declared as a global but is not set if called from method.
Re: Global variables in function
Posted: Tue Jun 10, 2008 5:39 pm
by Sonya
calguy1000 wrote:
This worked for me.
Code: Select all
<?php
include('include.php');
global $gCms;
$config =& $gCms->GetConfig();
print_r( $config );
?>
This works for me too, but I have to call it in the external class method and it breaks if included within the method.
Re: Global variables in function
Posted: Tue Jun 10, 2008 5:44 pm
by calguy1000
Code: Select all
<?php
include('include.php'); // include this here, so that things like $gCms etc, are in global scope
class Test {
function global_test() {
global $gCms;
$feusers =& $gCms->modules["FrontEndUsers"]["object"];
}
}
$instance = new Test();
$instance->global_test();
?>
You need to include the include.php at the global level
Re: Global variables in function
Posted: Tue Jun 10, 2008 5:53 pm
by Sonya
calguy1000 wrote:
Code: Select all
<?php
include('include.php'); // include this here, so that things like $gCms etc, are in global scope
class Test {
function global_test() {
global $gCms;
$feusers =& $gCms->modules["FrontEndUsers"]["object"];
}
}
$instance = new Test();
$instance->global_test();
?>
You need to include the include.php at the global level
Thank you, it works indeed
But I still do not understand why the variable is not available when called from method. I am going to read the link posted by Wiedmann again trying to understand the why global is not available within method.
Re: Global variables in function
Posted: Tue Jun 10, 2008 7:00 pm
by Wiedmann
But I still do not understand why the variable is not available when called from method.
That's the difference between "global $foo;" and "$GLOBALS['foo']" and if you assign references to $foo.
(CMSMS is using excessive "global" and references and so you have to do the include() in the global scope and not inside a function)
Re: Global variables in function
Posted: Tue Jun 10, 2008 7:37 pm
by Sonya
Wiedmann wrote:
But I still do not understand why the variable is not available when called from method.
That's the difference between "global $foo;" and "$GLOBALS['foo']" and if you assign references to $foo.
(CMSMS is using excessive "global" and references and so you have to do the include() in the global scope and not inside a function)
OK, I begin to understand. While including within class method I have $gCms and $config available as "just normal not global" variables. These variables are not added to $_GLOBALS because they are set within method. And all functions or methods referencing to "global" fail. I can only add variables to $_GLOBALS by setting them outside of class.
Re: Global variables in function
Posted: Tue Jun 10, 2008 8:56 pm
by Wiedmann
While including within class method I have $gCms and $config available as "just normal not global" variables.
No. If you include the CMSMS "include.php" inside a class method or function in your own script, the script just dies with an fatal error... It's a problem of the CMSMS core coding style that this not work.
These variables are not added to $_GLOBALS because they are set within method. And all functions or methods referencing to "global" fail.
That's part of the problem inside the CMSMS core.
BTW: It'S $GLOBALS and not $_GLOBALS.
I can only add variables to $_GLOBALS by setting them outside of class.
You can add vars to $GLOBALS from inside a class. But while also using references, you must make sure, that your global var (object) is really set in the global scope and not in the scope of that class method /function. Especially if other methodes/functions are refering to this var with "global". (and CMSMS is not doiing this).
That's the reason, why you must include the 'incude.php' in the global scope of your script. Because only then, $gCms, $config,... (and others) are really in the global scope and can be used from other methods/functions in the CMSMS core or your script.
Re: Global variables in function
Posted: Tue Jun 10, 2008 9:02 pm
by Sonya
Wiedmann, thank you! Good explaination. I guiess I understand and am a little bit wiser now
