By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

URGENT: variable scope in PHP classes

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.