473,386 Members | 1,609 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,386 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 2184
> ----- 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....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.