Page 1 of 1

Problem with global / $GLOBALS - Problem mit global bzw. $GLOBALS

Posted: Thu Apr 20, 2006 12:53 pm
by BlackMouse
Hi,

I need your help or an explanation to the following Problem:
I write an module and define some variables and try to use them in a function.
See the code in the german part.

This 08/15 code does not work with CMSMS.
I haven't found out how CMSMS handles gobal variables or how the $GLOBALS is modified.

Can someone explain it to me?

Thank you in advance,
Frank


Hallo,

ich benötige eure Hilfe, bzw. eine Erklärung zu folgendem Problem:

Ich schreibe gerade ein Modul und habe ein paar Daten ganz normal global definiert und möchte jetzt innerhalb einer Funktion darauf zugreifen. Also 08/15 PHP.
Als Beispiel test.php in action.globaltest.php
test.php

Code: Select all

<?php 
// test.php
$gTest = "Ich bin ein Test";

function fnTest()
{
  global $gTest;
  echo $gTest;
}
?>
action.globaltest.php

Code: Select all

<?php



include( 'include/test.php' );

fnTest(); 
?>
Das funktioniert aber nicht, und ich habe noch nicht rausgefunden wo bzw. wie CMSMS mit dem $GLOBALS Array umgeht.

Kann mir das jemand erklären?

Grüße,
Frank

Re: Problem with global / $GLOBALS - Problem mit global bzw. $GLOBALS

Posted: Thu Apr 20, 2006 2:00 pm
by BlackMouse
Ok,

I made a - IMO more or less - quick and dirty solution to this problem.

I figured out what the problem is - it's not a CMSMS problem, it's my problem ;) -  as i digged deeper into the code.

The whole process of loading the modules is done in a local scope of a function. So it couldn't work as I want my stuff to work.

What I do to use my variables as globals is as follows:

Take the test.php:

Code: Select all

// instead of
// $gTest = "Ich bin ein Test";
<?php
$GLOBALS['gTest'] = "Ich bin ein Test";

function fnTest()
{
  global $gTest; // refer to the defined gTest in the $GLOBALS array
  echo $gTest;
}
?>
In action.test.php

Code: Select all

<?php
include( 'include/test.php' );

fnTest(); 
?>
This will output what I want it to output.

Frank  :)