function using global object in PHP5 failes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arne Claus

    function using global object in PHP5 failes

    Hi.
    I've got a strange problem here with PHP5 (5.0.4). I've got file A,
    defining a class A and a file B, which sets up an Object of A (aObj) in
    the global scope like this.

    ----- A.php ----
    class A {
    //...
    }

    ----- B.php ----
    require_once "A.php";
    $aObj = new A();

    Ok - that's the setup so far. Now I include B.php in a file C.php,
    which lets me (correctly) acces aObj in the global scope, but not in a
    function defined there. And I _do_ use global:

    ----- C.php -----
    require_once "B.php";

    echo $aObj->some_member; // works

    function test() {
    global $aObj;
    echo $aObj->some_member; // does not work "Notice: Trying to get
    property of non-object"
    }

    Did I miss something here or did the behaviour of the global operator
    change in PHP5?
    Arne

  • Arne Claus

    #2
    Re: function using global object in PHP5 failes

    > ----- C.php -----[color=blue]
    > require_once "B.php";
    >
    > echo $aObj->some_member; // works
    >
    > function test() {
    > global $aObj;
    > echo $aObj->some_member; // does not work "Notice: Trying to get
    > property of non-object"
    > }[/color]

    Ok - spotted the problem.
    I use a function (which adds the base path to my require_once) for
    including files, defined as follows

    function require_framewo rk($file) {
    global $_BASEPATH; // defined somewhere above
    require_once $_BASEPATH.$fil e;
    }

    If I do the following in my main file main.php

    require_framewo rk("C.php");

    the error occures as described. If I include normally like

    require_once "C.php";

    everything works fine. So the problem occured because I created a
    function inside another function with this and thus the global keyword
    did not work.
    Well ... I wanted to discard that require_framewo rk function anyway ^^

    Arne

    Comment

    Working...