View Full Version : Using pass by reference, why the need for global variables?
Just wondering, in the case where a function needs to alter a variable outside itself, and passed as a parameter, is pass-by-reference always an acceptable and even better route to using global variables? For example:
Example 1- (pass by reference)
function test (&$myvar){
$myvar="george";
}
Example 2- (global)
function test ($myvar){
global $myvar;
$myvar="george";
}
In terms of OOP, is example 1 a better approach relative to 2? Thanks,
mordred
11-10-2002, 12:30 PM
Passing by reference is IMO better because the passed variable can be contained in another object's scope, instead of the global namespace which would be the case with using the "global" keyword.
firepages
11-10-2002, 01:31 PM
whatever Mordred said :)
most OOP programmers seem to regard Globals as evil for reasons that Mordred probably just explained, aside from that I just like the ease of use not having to always directly assign a value.
Spookster
11-11-2002, 07:00 AM
In OOP you do not want to give global access to data members within objects to everyone. That is just asking for trouble. Especially if this is a program being written by multiple people. If you want someone to be able to access a data member then provide that ability locally within the object but don't just hand out global access.
I hope that PHP works on the OOP functionality to the point where data members or functions can be declared private within an object. Java is a good example of the way OOP should be.
firepages
11-11-2002, 08:00 AM
PHP should see Private variables in the next Major version , but some will suggest that the current 'PHP coding standards' (http://pear.php.net ) suffice....
i.e. variables begining with an _ for privates & _T for protected variables, any collaberative group sticking to these rules (and they have to confirm to some!) should in general have no issues.
<?
class some_class {
var $_private_variable;
var $_Tprotected_variable;
}
?>
utilising these standards its quite easy to check for private variables before messing with them.
mordred
11-11-2002, 08:10 AM
Here at our company we have committed ourselves to code PHP according to the PEAR coding standards. Although this gives us a common way to signal other programmeres which variable is private/protected, it's still a kludge, and reminds me of Hungarian Notation, which I find terribly redundant in an OOP language. You still can directly manipulate any class/object variables from anywhere in your script, if you are too lazy writing getters/setters. But anyway, it's better than nothing and provides a standardized path to address this problem.
Of course, for a quick hack or prototype this isn't really necessary.
hybridsun
04-04-2003, 01:54 AM
Can you append a variables name inside a function with the value of the parameter passed in the function?
Spookster
04-04-2003, 04:29 AM
Why would you want to append anything to the variables name?
hybridsun
04-04-2003, 05:35 AM
I have a function that works beatifully. However it has hard coded
values in it defining the layer.style in it. Since Im using this
function 5 or 6 times on the page I want to consolidate the function
into one function. Heres the thing. I have labeled my layers: menu0,
menu1, menu2, etc.) So I think it should be possible to change the
variable called in my function (block0, block1, block2) using the
paramter.
Any insight would be nice.
Here's some sample code:
function init() {
if (ns4) block0 = document.menu0
if (ie4) block0 = menu0.style
block0.ypos = parseInt(block0.top)
if (ns4) block1 = document.menu1
if (ie4) block1 = menu1.style
block1.ypos = parseInt(block1.top)
if (ns4) block2 = document.menu2
if (ie4) block2 = menu2.style
//........................ETC, ETC
}
function slidemenu0up() {
if (ie4){
// alert ('IE, damn you for supporting microsoft but its the only
choice')
if (block0.ypos > 15) {
block0.ypos -= 8
block0.top = block0.ypos
setTimeout("slidemenu0up()",30)
}
}
else {
// alert ('navigator sucks')
moveLayerTo('menu0',333,15)
}
}
So to recap I want to pass the parameter 1,2,3 etc like slidemenuup(1)
but Im having some problems.
I tried this: but it didnt work
function slidemenuup(menuNo) {
if (ie4){
if (("block"+menuNo+".ypos") > 15) {
eval("block"+menuNo+".ypos") -= 8
("block"+menuNo).top = ("block"+menuNo).ypos
setTimeout("slidemenuup(menuNo)",30)
}
}
else {
moveLayerTo('menu2',333,15)
}
}
Thanks in advance!
that's javascript and nothing to do with passing variables or references in php.
anyway - you could iterate from n_count upto your maximum and eval the required functionality even in javascript
n_count = -1;
while(++n_count <= block.length)
{
(ns4) ? eval("block" +n_count+ " = document.menu" +n_count+";") : eval("block" +n_count+ " = menu" +n_count+".style;");
eval("block" +n_count+ ".ypos = parseInt(block" +n_count+ ".top);");
}
flytyped - should point you in the right direction though -
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.