Global variables in function

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
Sonya

Global variables in function

Post 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
Wiedmann
Forum Members
Forum Members
Posts: 233
Joined: Wed Mar 26, 2008 1:49 am
Location: Stuttgart / Germany

Re: Global variables in function

Post 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'];
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Global variables in function

Post 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();
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Sonya

Re: Global variables in function

Post 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.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Global variables in function

Post by calguy1000 »

This worked for me.

Code: Select all

<?php

include('include.php');
global $gCms;
$config =& $gCms->GetConfig();
print_r( $config );

?>
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Sonya

Re: Global variables in function

Post 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.
Last edited by Sonya on Tue Jun 10, 2008 5:40 pm, edited 1 time in total.
Sonya

Re: Global variables in function

Post 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.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Global variables in function

Post 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
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Sonya

Re: Global variables in function

Post 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.
Last edited by Sonya on Tue Jun 10, 2008 6:02 pm, edited 1 time in total.
Wiedmann
Forum Members
Forum Members
Posts: 233
Joined: Wed Mar 26, 2008 1:49 am
Location: Stuttgart / Germany

Re: Global variables in function

Post 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)
Sonya

Re: Global variables in function

Post 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.
Wiedmann
Forum Members
Forum Members
Posts: 233
Joined: Wed Mar 26, 2008 1:49 am
Location: Stuttgart / Germany

Re: Global variables in function

Post 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.
Sonya

Re: Global variables in function

Post by Sonya »

Wiedmann, thank you! Good explaination. I guiess I understand and am a little bit wiser now :)
Post Reply

Return to “Developers Discussion”