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

URGENT: variable scope in PHP classes

Hi everyone

I am having some problems with variable scope in my php classes. I thought
varibles declared in classes were visble throughout the class, but I am
finding this is not the case.

What is happening is that when functions in my class go to access variables
they are returning nothing even though they have had values assigned..

Any idea what is going on?

Thanks

Hamilton

www.laughland.biz
"the web site for web sites?

Jul 16 '05 #1
4 6250
Spidah <ha******@eyehug.co.nz> kirjoitti:
What is happening is that when functions in my class go to access variables
they are returning nothing even though they have had values assigned..


You must have something wrong in your code. :) An example would have
helped a lot.

What do you mean with a variable - a common variable or an attribute?
Attributes are available in all functions (methods) of the class, but
common variables only in the functions they are declared in.

class foo {
var $bar = 'foobar'; // attribute

function example() {
print $this->bar; // prints foobar
$variable = 'cheese'; // variable
}

function example2() {
print $variable; // prints nothing
}
}

--
Antti Lahtonen
http://www.iki.fi/andu/
Jul 16 '05 #2
I was talking about attributes rather than common variables.

It is my understanding that, as you stated, they are available to every
method of the class. I assume this is the case regardless of what method
assigns the value to that attribute?

For example:

class MyClass{

var $myvar;

function one (){
$this->myvar = "foobar";
}

function two(){
echo $this->myvar;
}

function three(){
$temp = "this is a ";
return $temp . $this->myvar;
}
}

What I am finding as that in some instances two() is sending nothing to the
browser, even if one() has been called first. If I use myvar in another
method, in this example three(), between the calls to one() and two(), then
even though three() contains no code to alter the value of myvar, the
subsequent call to two() returns nothing.

I gather I have just done something wrong in my code and that?

Hamilton
"Antti Lahtonen" <an******@verso.st.jyu.fi> wrote in message
news:bi**********@mordred.cc.jyu.fi...
Spidah <ha******@eyehug.co.nz> kirjoitti:
What is happening is that when functions in my class go to access variables they are returning nothing even though they have had values assigned..


You must have something wrong in your code. :) An example would have
helped a lot.

What do you mean with a variable - a common variable or an attribute?
Attributes are available in all functions (methods) of the class, but
common variables only in the functions they are declared in.

class foo {
var $bar = 'foobar'; // attribute

function example() {
print $this->bar; // prints foobar
$variable = 'cheese'; // variable
}

function example2() {
print $variable; // prints nothing
}
}

--
Antti Lahtonen
http://www.iki.fi/andu/

Jul 16 '05 #3
On Mon, 1 Sep 2003 09:21:58 +1200
"Spidah" <ha******@eyehug.co.nz> wrote:

<snip>
class MyClass{

var $myvar;

function one (){
$this->myvar = "foobar";
}

function two(){
echo $this->myvar;
}

function three(){
$temp = "this is a ";
return $temp . $this->myvar;
}
}

What I am finding as that in some instances two() is sending nothing
to the browser, even if one() has been called first. If I use myvar in
another method, in this example three(), between the calls to one()
and two(), then even though three() contains no code to alter the
value of myvar, the subsequent call to two() returns nothing.

I gather I have just done something wrong in my code and that?


<snip>

There seems to be nothing wrong with this class. I have run the code
here and all is fine. The output is what one would expect. You may
have a problem in your real class.

Matt
--
Quispiam Power Computing | "There are two major products that come out
Pendle Hill, Australia | of Berkeley: LSD and UNIX. We don't believe
+61 2 9631 7719 | this to be a coincidence. "
www.quispiam.com | - Jeremy S. Anderson
Jul 16 '05 #4
Spidah <ha******@eyehug.co.nz> kirjoitti:
It is my understanding that, as you stated, they are available to every
method of the class. I assume this is the case regardless of what method
assigns the value to that attribute?
Yes. Of course the method has to be called first.
For example:
[..]
I gather I have just done something wrong in my code and that?


The class seems to be ok. Probably you have an error somewhere where you
are calling instances of that class.

Remember to use:
error_reporting(E_ALL);
as the first line of your code - it helps finding problems related to
empty variables.
--
Antti Lahtonen
http://www.iki.fi/andu/
Jul 16 '05 #5

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

Similar topics

6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
3
by: Thomas Matthews | last post by:
Hi, While coding programs, I cam about a conundrum regarding variables defined in an iterative loop. The issue is whether it is more efficient to factor the definition out of the loop or...
0
by: Joe Blow via DotNetMonster.com | last post by:
Hello, I have a design problem involving class instances as global variables. To give you my background, I've programmed lots in Java, and most of it has been large class structures. I'm...
7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
6
by: Joe Molloy | last post by:
Hi, I'm wondering is there any way I can get a variable's value from within a class when the variable has been declared outside the class but in the same script as the class is contained in. ...
1
by: Just Me | last post by:
Given a file containing Multiple classes and a variable that needs to be seen in all the classes I can put it in one of the classes as Shared and reference it the other classes as ...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
77
by: Hans Schneider | last post by:
1. in the prg bellow what vars are stored on stack, heap, data segment? int i; void main() { int j; int *k = (void *)malloc(1); }
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: 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
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: 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
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.