473,385 Members | 2,069 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,385 software developers and data experts.

Global variables without the global keyword?

I have various classes I want available everywhere in my scripts. At the moment, I have something along the lines of the following:

[PHP]$aClass = new aClass();

class anotherClass {
function aMethod() {
global $aClass;
return $aClass->anotherMethod();
}
}[/PHP]

I want to achieve the same result without the global keyword, maybe that's just wishful thinking..?

Anyway, thanks for your help in advance
Mar 19 '08 #1
4 2205
TheServant
1,168 Expert 1GB
Have you thought about using sessions? Why don't you want the global word?
Mar 19 '08 #2
Thanks for the reply.. I have thought about sessions, yeah. And that might be the option I go for.

The reason I don't want to use the global keyword is that I want to have certain variables throughout all functions and files of the program instantly available.

I think i'm just being too optimistic, but maybe there's a way to do that..?

Thanks again
Mar 25 '08 #3
code green
1,726 Expert 1GB
I think you need to study the basic theory behind classes
I have various classes I want available everywhere in my scripts
. Your code is using a class object not a class. [PHP]class anotherClass {
function aMethod() {
global $aClass;
return $aClass->anotherMethod();
}
}[/PHP] To make a class available simply create another class object.
want to achieve the same result without the global keyword, maybe that's just wishful thinking..?
I cannot work out the result you are trying to achieve.
But if you want a class object available in another class then pass it's address as a parameter [PHP]class anotherClass {
function aMethod(&$aClass) {}[/PHP] If you want a class to use another class method or member then they need to be declared as static.
If you want a class to do everything another class can then consider inheritance.
At least you recognise even if by accident, that globals are bad practice.
Mar 25 '08 #4
I think you need to study the basic theory behind classes . Your code is using a class object not a class. [PHP]class anotherClass {
function aMethod() {
global $aClass;
return $aClass->anotherMethod();
}
}[/PHP] To make a class available simply create another class object. I cannot work out the result you are trying to achieve.
But if you want a class object available in another class then pass it's address as a parameter [PHP]class anotherClass {
function aMethod(&$aClass) {}[/PHP] If you want a class to use another class method or member then they need to be declared as static.
If you want a class to do everything another class can then consider inheritance.
At least you recognise even if by accident, that globals are bad practice.

Yeah, I do realise that globals aren't good practice. It's for that reason and partly laziness that I'd prefer not too use them. All I'm basically trying to do is avoid the scope issue that inside a function (or method in this case) you can't use exterior variables without any extra work..

Thanks for your help anyway
Mar 27 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: A | last post by:
Hi, Consider this code: //Header File - Foo.h int i = 0; // non-static global variable class Foo{ ...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
7
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope)...
10
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of...
23
by: Anil Gupte | last post by:
I am trying to set up a function that connects to the database that I can then use gloablly. I set up a class called L3Global in which I have a function as follows: Public Function...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.