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

Exposing Variables

I've been using global to expose variables. However, I noticed
the following...

(1) global seems to use the nearest declaration.

For example,

// module1.php
$sMyString = "Hello World.";

function Function1($sItemToProcess1) {
global $ALL_GETS, $sMyString;
// Above references $ALL_GETS in a different module.
// ...
// code to process some things
$sItemToProcess2 = $sItemToProcess1 . " " . $sMyString;
// pass in the new variable to the second function...
$sCheckThis = Function2($sItemToProcess2)
}

function Function2($sItemToProcess) {
global $ALL_GETS; // this one references $ALL_GETS in...
// Function1(), not $ALL_GETS in the originating module...

// I used different variable names when I noticed that global
// also references variables defined in a calling procedure.
//
// PHP 4.4.4 (cgi-fcgi) (built: Aug 16 2006 01:17:43)
//

// I accidently discovered this while creating a variable inside
// one function and was thinking that I'd need to put it outside
// the function and use the global keyword to make it visible.
//
// However, PHP seems to allow creation inside one function,
// then makes that creation visible via the global keyword in
// another function.
//

// ... code to process things ...
}

Meanwhile, both of the functions retain visibility (but not so for the
variables declared in those included/required modules), e.g...

// module2.php
require("/include/module1.php");

$ALL_GETS = array_change_key_case($_GET, CASE_LOWER);
$sThisItem = "blahblah_example";
$sThisItem2 = Function1($sThisItem);

I'm seeing that I can use a function name to return a variable and
make the variable visible...

// module3.php
$sThisIsPrivateTag = "This is sThisIsPrivateTag.";

function PrivateTagGetContents() {
return($sThisIsPrivateTag);
}

function PrivateTagUpdateContents($sUpdate) {
$sThisIsPrivateTag = $sUpdate;
// return($sThisIsPrivateTag);
}

That in effect gets rid of the need for using the global keyword.

I haven't dug into object use inside of PHP and I'm looking for
alternatives to using global.

I appreciate any comments/suggestions/alternatives.

Thanks.

--
Jim Carlock
Post replies to the group.
Dec 26 '06 #1
4 1665
IMHO simplest solution is using $_GLOBAL.

Hendri

Jim Carlock wrote:
I've been using global to expose variables. However, I noticed
the following...

(1) global seems to use the nearest declaration.

For example,

// module1.php
$sMyString = "Hello World.";

function Function1($sItemToProcess1) {
global $ALL_GETS, $sMyString;
// Above references $ALL_GETS in a different module.
// ...
// code to process some things
$sItemToProcess2 = $sItemToProcess1 . " " . $sMyString;
// pass in the new variable to the second function...
$sCheckThis = Function2($sItemToProcess2)
}

function Function2($sItemToProcess) {
global $ALL_GETS; // this one references $ALL_GETS in...
// Function1(), not $ALL_GETS in the originating module...

// I used different variable names when I noticed that global
// also references variables defined in a calling procedure.
//
// PHP 4.4.4 (cgi-fcgi) (built: Aug 16 2006 01:17:43)
//

// I accidently discovered this while creating a variable inside
// one function and was thinking that I'd need to put it outside
// the function and use the global keyword to make it visible.
//
// However, PHP seems to allow creation inside one function,
// then makes that creation visible via the global keyword in
// another function.
//

// ... code to process things ...
}

Meanwhile, both of the functions retain visibility (but not so for the
variables declared in those included/required modules), e.g...

// module2.php
require("/include/module1.php");

$ALL_GETS = array_change_key_case($_GET, CASE_LOWER);
$sThisItem = "blahblah_example";
$sThisItem2 = Function1($sThisItem);

I'm seeing that I can use a function name to return a variable and
make the variable visible...

// module3.php
$sThisIsPrivateTag = "This is sThisIsPrivateTag.";

function PrivateTagGetContents() {
return($sThisIsPrivateTag);
}

function PrivateTagUpdateContents($sUpdate) {
$sThisIsPrivateTag = $sUpdate;
// return($sThisIsPrivateTag);
}

That in effect gets rid of the need for using the global keyword.

I haven't dug into object use inside of PHP and I'm looking for
alternatives to using global.

I appreciate any comments/suggestions/alternatives.

Thanks.
Dec 26 '06 #2
Jim Carlock wrote:
I've been using global to expose variables. However, I noticed
the following...

(1) global seems to use the nearest declaration.

For example,

// module1.php
$sMyString = "Hello World.";

function Function1($sItemToProcess1) {
global $ALL_GETS, $sMyString;
// Above references $ALL_GETS in a different module.
// ...
// code to process some things
$sItemToProcess2 = $sItemToProcess1 . " " . $sMyString;
// pass in the new variable to the second function...
$sCheckThis = Function2($sItemToProcess2)
}

function Function2($sItemToProcess) {
global $ALL_GETS; // this one references $ALL_GETS in...
// Function1(), not $ALL_GETS in the originating module...

// I used different variable names when I noticed that global
// also references variables defined in a calling procedure.
//
// PHP 4.4.4 (cgi-fcgi) (built: Aug 16 2006 01:17:43)
//

// I accidently discovered this while creating a variable inside
// one function and was thinking that I'd need to put it outside
// the function and use the global keyword to make it visible.
//
// However, PHP seems to allow creation inside one function,
// then makes that creation visible via the global keyword in
// another function.
//

// ... code to process things ...
}

Meanwhile, both of the functions retain visibility (but not so for the
variables declared in those included/required modules), e.g...

// module2.php
require("/include/module1.php");

$ALL_GETS = array_change_key_case($_GET, CASE_LOWER);
$sThisItem = "blahblah_example";
$sThisItem2 = Function1($sThisItem);

I'm seeing that I can use a function name to return a variable and
make the variable visible...

// module3.php
$sThisIsPrivateTag = "This is sThisIsPrivateTag.";

function PrivateTagGetContents() {
return($sThisIsPrivateTag);
}

function PrivateTagUpdateContents($sUpdate) {
$sThisIsPrivateTag = $sUpdate;
// return($sThisIsPrivateTag);
}

That in effect gets rid of the need for using the global keyword.

I haven't dug into object use inside of PHP and I'm looking for
alternatives to using global.

I appreciate any comments/suggestions/alternatives.

Thanks.
Jim,

It's better not to use globals. Rather, pass the needed values in the
function call. Much better programming practice.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 27 '06 #3
::Jerry Stuckle:: posted...
: It's better not to use globals. Rather, pass the needed values in
: the function call. Much better programming practice.

Thanks Jerry and Hendri.

I ran into a problem using "global" where I copied a variable name
outside of a function and even though I didn't put anything in the
outside variable.

When creating a class in PHP 4, I'm not seeing any kind of
private variables. Is there a way to simulate private variables
in PHP 4 ?

I've been thinking about moving to PHP 5.x, but before I finalize
and commit to such... I'm exploring classes in PHP 4.

--
Jim Carlock
Post replies to the group.
Dec 28 '06 #4
Jim Carlock wrote:
::Jerry Stuckle:: posted...
: It's better not to use globals. Rather, pass the needed values in
: the function call. Much better programming practice.

Thanks Jerry and Hendri.

I ran into a problem using "global" where I copied a variable name
outside of a function and even though I didn't put anything in the
outside variable.

When creating a class in PHP 4, I'm not seeing any kind of
private variables. Is there a way to simulate private variables
in PHP 4 ?

I've been thinking about moving to PHP 5.x, but before I finalize
and commit to such... I'm exploring classes in PHP 4.
Sorry, there really isn't. PHP 5 has much better encapsulation.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 28 '06 #5

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

Similar topics

4
by: James Henson | last post by:
I'm using a MySQL database from within some Perl and PHP cgi's. To make the connection, I have to supply the username/password in the connection string. This info is readable for anyone that can...
3
by: Prigozhin Roman | last post by:
But if I do this, then later in the code where I'm implementing set and get functions I'm getting error : Class already contains a definition for 'MyProperty'. Probably I'm not implementing...
4
by: Peter Hemmingsen | last post by:
Hi, I have a dotnet object (implemented in mc++ and used in c#) which have a property called "Info". The Info property is also a dotnet object (implemented in mc++). In the constructor of the...
3
by: bill | last post by:
I need to open a asp.net web form from a classic asp page, and pass a username and password to the asp.net page. The username and password exist as session variables in the classic asp...
8
by: Dave A | last post by:
I have a class called 'PrimaryKey' that represents the primary key of a table. PrimaryKeys can only be created and the class only implements .ToString(). The PrimaryKey class internally stores...
2
by: Neil Cerutti | last post by:
I'm using doctest for the first time, and boy is it cool. But I'm nervous about exposing library internals in the docstring. def glk_cancel_char_event(win): """ Cancel a pending request for...
4
by: =?Utf-8?B?QkogU2FmZGll?= | last post by:
We have a class that has a public property that is of type List<T>. FXCop generates a DoNotExposeGenericLists error, indicating "System.Collections.Generic.List<Tis a generic collection designed...
4
by: =?Utf-8?B?Y2hyaXNjYXA=?= | last post by:
I tried searching the forums for this already and didn't find anything. I want to expose functionality from a vb6 COM component via a WCF service. I am trying to evaluate if exposing this...
1
by: bantunks | last post by:
Hello, I am trying to figure out the advantages and disadvantages of exposing interfaces through Opaque data types in C. I have figured/found out the following two advantages 1. Higher level of...
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
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...

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.