Var will not show up in globals!!??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • www.douglassdavis.com

    Var will not show up in globals!!??

    For example, if I put this code in a file called globaltest.php:

    $new_var="This is a string";

    class TestClass
    {

    function __construct()
    {
    global $new_var;
    echo "<br>new var: ".$new_var;
    }

    function testFunc()
    {
    global $new_var;
    echo "<br>new var in testFunc(): ".$new_var;
    echo "<br>from globals ".$GLOBALS['new_var'];
    }
    }

    $testo=new TestClass();
    $testo->testFunc();

    It displays

    new var: This is a string
    new var in testFunc(): This is a string
    from globals This is a string

    as it should.

    However, when I put it in another file that is an included php file, it
    doesn't even register new_var as a global, and I get this printed out:

    new var:
    new var in testFunc():
    from globals

    I don't understand. What could possibly be making this happen? Right
    now I am looking for any possibilities, because to me, it doesn't seem
    to make sense.

    I am using 5.0.4, but, the same happens in 5.0.5

  • Carl

    #2
    Re: Var will not show up in globals!!??

    www.douglassdavis.com wrote:[color=blue]
    > For example, if I put this code in a file called globaltest.php:
    >
    > $new_var="This is a string";
    >
    > class TestClass
    > {
    >
    > function __construct()
    > {
    > global $new_var;
    > echo "<br>new var: ".$new_var;
    > }
    >
    > function testFunc()
    > {
    > global $new_var;
    > echo "<br>new var in testFunc(): ".$new_var;
    > echo "<br>from globals ".$GLOBALS['new_var'];
    > }
    > }
    >
    > $testo=new TestClass();
    > $testo->testFunc();
    >
    > It displays
    >
    > new var: This is a string
    > new var in testFunc(): This is a string
    > from globals This is a string
    >
    > as it should.
    >
    > However, when I put it in another file that is an included php file, it
    > doesn't even register new_var as a global, and I get this printed out:
    >
    > new var:
    > new var in testFunc():
    > from globals
    >
    > I don't understand. What could possibly be making this happen? Right
    > now I am looking for any possibilities, because to me, it doesn't seem
    > to make sense.[/color]

    Are you including the include file in the global scope, outside of any
    classes/methods?
    Are you defining the variable you are attempting to access in the global
    scope as well?
    Post the code that doesn't work, not the one that does.

    Carl.
    [color=blue]
    >
    > I am using 5.0.4, but, the same happens in 5.0.5
    >[/color]

    Comment

    • www.douglassdavis.com

      #3
      Re: Var will not show up in globals!!??


      Carl wrote:
      [color=blue]
      > Are you including the include file in the global scope, outside of any
      > classes/methods?
      > Are you defining the variable you are attempting to access in the global
      > scope as well?
      > Post the code that doesn't work, not the one that does.
      >
      > Carl.[/color]

      that's the code that doesn't work. It doesn't work when included, does
      work when it is in a standalone file.

      But thanks for the help... I thought i had included it from the global
      scope, but, turns out i had included it from another scope previously
      some where else in the code.

      Comment

      Working...