473,236 Members | 1,316 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,236 software developers and data experts.

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

Jul 17 '05 #1
1 2179
> ----- 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"
}


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_framework($file) {
global $_BASEPATH; // defined somewhere above
require_once $_BASEPATH.$file;
}

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

require_framework("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_framework function anyway ^^

Arne

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
6
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
0
by: Tim Meader | last post by:
Hello, I was hoping someone could provide some insight as to the best approach to take in porting over a function I use in quite a few spots from PHP4 to PHP5. Basically, I have a db wrapper class...
4
by: Bit Byte | last post by:
Noobie here (C++/C/Java experience though) ... Recently picked up PHP ... I want to pass an array to a function and then to use count on the passed variable - is this the way to do it (its an...
0
by: Erwin Moller | last post by:
Hi group, I found something strange in PHP5.2.4 (on IIS7/Vista). I am working on an app that has been running just fine under heavy load for over a year at some custumer of mine. (they have...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.