473,378 Members | 1,377 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,378 software developers and data experts.

classes and variable scope

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. For example, say I have the following
script

<?php
constant myvar = "my original string";

class myClass
{
function returnmyvar()
{
return "some processed text and ".$myvar;
}

function usesnewstr()
{
echo $this->returnmyvar();
}
}

$myCls = &New myClass($myvar);
$myCls -> usesnewstr();
?>
Now what I'd like this to output would be:
some processed text and my original string
but all I get is:
some processed text and

Now I know I could make a constructor function and pass $myvar in that way
but I would rather not have to do that as that would entail changing the
instatiation statements whevever that class is already used. Any other ways
to make $myvar visible to functions inside the class?

Joe
Nov 3 '05 #1
6 2256
First of all, in the example you provided, myvar is a constant, not a
variable. So if you ever try to get to it with $myvar you'll likely
get some sort of error or warning.

Constants and variables behave differently when looking at them from
another scope. When you define a constant in the global scope (that
is, outside of a class or function), it is available
everywhere...period. When you define a variable in the global scope,
it is available in the global scope as you would expect.

However, when you are in a different scope, such as a function or
class, global variables won't just "show up" automatically. You can
get to it, however, using either of the following methods:

//First method:
global $myvar; //$myvar is the global myvar
$x = $myvar; //$x gets the value from the global variable "myvar"

//Second method:
$x = $_GLOBALS['myvar']; //$x gets the value from the global variable
"myvar"

While I'm not entirely positive, I believe that the first one makes the
local $myvar a *reference* to the global $myvar. In other words, if
you change the value of $myvar from within that function and return to
the global scope, you'll find that $myvar has the new value.

If myvar is a constant, you can get to him from anywhere (the script, a
class definition, or a function) by just writing:

$x = myvar; //no $ since myvar is a constant

Nov 3 '05 #2
you can always use
(in PHP4)

class myClass{
function myClass($myvar = 'my original string'){
echo 'whatever ' . $myvar;
}
}

or something like that

Nov 3 '05 #3
Joe Molloy wrote:
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. For example, say I have the following
script

<?php
constant myvar = "my original string";

class myClass
{
function returnmyvar()
{
return "some processed text and ".$myvar;
}

function usesnewstr()
{
echo $this->returnmyvar();
}
}

$myCls = &New myClass($myvar);
$myCls -> usesnewstr();
?>
Now what I'd like this to output would be:
some processed text and my original string
but all I get is:
some processed text and

Now I know I could make a constructor function and pass $myvar in that way
but I would rather not have to do that as that would entail changing the
instatiation statements whevever that class is already used. Any other ways
to make $myvar visible to functions inside the class?

Joe

how about
function returnmyvar(){
return "some processed text and ". $this->myvar;
}

Nov 3 '05 #4
Thanks for all the tips - the $_GLOBALS function looks the most applicable
in my situation - I'm comming into some previously written code and don't
want to have to reinvent the wheel :)

Joe

"Joe Molloy" <mo********@hotmail.com> wrote in message
news:Hr*****************@news.indigo.ie...
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. For example, say I have the
following script

<?php
constant myvar = "my original string";

class myClass
{
function returnmyvar()
{
return "some processed text and ".$myvar;
}

function usesnewstr()
{
echo $this->returnmyvar();
}
}

$myCls = &New myClass($myvar);
$myCls -> usesnewstr();
?>
Now what I'd like this to output would be:
some processed text and my original string
but all I get is:
some processed text and

Now I know I could make a constructor function and pass $myvar in that way
but I would rather not have to do that as that would entail changing the
instatiation statements whevever that class is already used. Any other
ways to make $myvar visible to functions inside the class?

Joe

Nov 4 '05 #5
Actually, in the end, the code that worked looked like this for anyone else
in a similar situation:

<?php
$myvar = "my original string";

class myClass
{

var $mystr;

function myClass()
{
global $myvar;
$this -> mystr = $myvar;
}

function returnmyvar()
{
return "some processed text and ".$this -> mystr;
}

function usesnewstr()
{
echo $this->returnmyvar();
}
}

$myCls = &New myClass();
$myCls -> usesnewstr();
?>

Next week, inheritence ;)

Joe
"Joe Molloy" <mo********@hotmail.com> wrote in message
news:Hr*****************@news.indigo.ie...
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. For example, say I have the
following script

<?php
constant myvar = "my original string";

class myClass
{
function returnmyvar()
{
return "some processed text and ".$myvar;
}

function usesnewstr()
{
echo $this->returnmyvar();
}
}

$myCls = &New myClass($myvar);
$myCls -> usesnewstr();
?>
Now what I'd like this to output would be:
some processed text and my original string
but all I get is:
some processed text and

Now I know I could make a constructor function and pass $myvar in that way
but I would rather not have to do that as that would entail changing the
instatiation statements whevever that class is already used. Any other
ways to make $myvar visible to functions inside the class?

Joe

Nov 4 '05 #6
That's fine but it's worth noting that, once myClass has been
instantiated, any changes to $myvar in the global scope will not be
reflected in the member variable of the object. This can be changed by
changing:

$this -> mystr = $myvar;

in the constructor to:

$this->mystr =& $myvar;

I think that should work -- although I didn't test it.

Nov 5 '05 #7

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

Similar topics

4
by: Spidah | last post by:
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. ...
24
by: Alf P. Steinbach | last post by:
The eighth chapter (chapter 2.1) of my attempted Correct C++ tutorial is now available, although for now only in Word format -- comments welcome! Use the free & system-independent Open Office...
12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
0
by: Ewart MacLucas | last post by:
generated some WMI managed classes using the downloadable extensions for vs2003 from mircrosoft downloads; wrote some test code to enumerate the physicall processors and it works a treat, but a...
5
by: JohnR | last post by:
in VB.NET I'm trying to access my variable without resorting to SHARED scope. Here's the situation: class MYLABEL inherits LABEL and has an additional property called MYVAR. In MYLABEL I set an...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
3
by: r0g | last post by:
Hi There, I'm refactoring some old code that uses global variables and was originally written in one big flat file with a view to nicening it up and then extending it. The problem I have though...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.