SitePoint Sponsor |
|
User Tag List
Results 1 to 19 of 19
Thread: Passing by reference
-
Sep 26, 2003, 08:34 #1
- Join Date
- Aug 2003
- Location
- Singapore
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Passing by reference
Hi
I want to know when I should pass by reference (in oop), can anyone provide an answer for that? Also, everything in the constructor of a class gets executed like in procedural programming am I right (unlike the other functions which only execute when they are called?)
-
Sep 26, 2003, 09:23 #2
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Umm.... Kind of hard for me to explain, though I'll instead show some example script ?
PHP Code:.
.
$dom = domxml_new_doc('1.0');
$pageWidget = & new pageWidget(& $dom);
$pageWidget -> create(); // page skeleton
#
$title = & new titleWidget(& $dom);
$title -> create($action['PAGE.TITLE']);
$pageWidget -> addHead($title -> fetch());
$style = & new cssLinkWidget(& $dom);
$style -> create($css);
$pageWidget -> addHead($style -> fetch());
#
$pageGenerator = & new $this -> view(& $pageWidget, & $dom);
#
echo($pageWidget -> show());
.
.
Whereas If I hadn't used references on the object pageGenerator and it's parameters then the content generated by this class [pageGenerator] wouldn't I believe be passed back automatically.
Also on your second point about a class constructor ? You are correct
Hope this has helped to explain references; maybe reading about references over at HarryF's site would also help you ?
www.phppatterns.com
-
Sep 26, 2003, 18:43 #3
- Join Date
- Aug 2003
- Location
- Singapore
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Still not sure, but do references help to increase efficiency?
edit: after reading around, found out the advantages, but where would it's practical uses be? And can someone explain why would I return a reference? Thanks!Last edited by The Red Guy; Sep 26, 2003 at 19:09.
-
Sep 27, 2003, 07:47 #4
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Umm... You do not return a reference; You just use it
The
PHP Code:&
Better then to pass this data by references as apposed to passing by a copy of the data no ?
-
Sep 27, 2003, 09:14 #5
- Join Date
- May 2003
- Location
- back in South Africa
- Posts
- 378
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
References are used in the following situations:
- to avoid passing a copy (of a large amount) of data (see. expl. of Dr Livingston), hence the execution of the script is boosted
- to be in the position to manipulate the original container of the data ... pay attention!
Originally Posted by Dr Livingston
PHP Code:function & returnReference() {
// do something and return a variable
}
.
-
Sep 27, 2003, 11:09 #6
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
if you define the signature of the function/method as
Thanks Stefano F for pointing that out
-
Sep 28, 2003, 04:51 #7
- Join Date
- Aug 2003
- Location
- Singapore
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Let's say I've a method getObject ($param), and another displayObject ($param). How do I make $param modify to a variable in a class, and then affect the other? Like if I input $param to be 'foo', then can I pass the reference to $this->param, then in turn the $param in displayObject() can be passed by reference instead? How do I do that? Thanks!
-
Sep 29, 2003, 05:21 #8
- Join Date
- Aug 2003
- Location
- Singapore
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Also, can anyone explain what goes on in something like while (list($key,$value) = each($array))?
-
Sep 29, 2003, 05:41 #9
- Join Date
- Oct 2002
- Location
- Iceland
- Posts
- 1,238
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by The Red Guy
PHP Code:foreach ($array as $key => $value) { }
- website
-
Sep 29, 2003, 06:58 #10
- Join Date
- Aug 2003
- Location
- Singapore
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by website
Originally Posted by The Red Guy
-
Sep 29, 2003, 08:28 #11
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Umm.... If I remember properly from the manual ?
PHP Code:.
.
list($a, $b, $c, $d) = each(mysql_fetch_array($sql));
.
.
As to you original question it's too confusing for me to get my head around at the moment...
I'm seriously lacking some decent sleep at the moment
-
Sep 29, 2003, 13:52 #12
- Join Date
- Oct 2002
- Location
- Iceland
- Posts
- 1,238
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Dr, I belive this does not work, or at least you wouldn't get the desired results.
What does each() do?
each() takes the element which the internal pointer of the array points to and returns its key and value as an array on the form of
[0] => key
[1] => value
[key] => key
[value] => value
then it increments the internal pointer (then to the next element).
Example:
PHP Code:$array = array('value1', 'value2', 'key3' => 'value3');
//now the pointer of the array points to the first element
$arr1 = each($array);
/*
print_r($arr1) would show something like:
Array(
[0] => 0
[1] => value1
[key] => 0
[value] => value1
)
*/
$arr2 = each($array);
/*
print_r($arr2) would show something like:
Array(
[0] => 1
[1] => value2
[key] => 1
[value] => value2
)
*/
$arr3 = each($array);
/*
print_r($arr3) would show something like:
Array(
[0] => key3
[1] => value3
[key] => key3
[value] => value3
)
*/
I have rarely used each() although it does happen, it could be useful when you need some for loop or something. Imagine something like this:PHP Code:for ($i = 0; $i < count($arr); $i++) {
list($key, $value) = each($arr);
/* do something */ }
PHP Code:$sql = 'SELECT col1, col2, col3 FROM table';
$rslt = mysql_query($sql);
//because mysql_fetch_row returns the columns as keys in the array we'll use that (mysql_fetch_array can also be used);
while (list($col1, $col2, $col3) = mysql_fetch_row($rslt)) { }
PHP Code:class Test {
var $test;
function assignTest($test) {
$this->test =& $test;
}
function printTest() {
echo $this->test;
}
}
$test = 'A';
$obj = new Test();
$obj->assignTest(&$test);
$test = 'B';
$obj->printTest();
//will echo B
- website
-
Sep 30, 2003, 00:53 #13
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
while (list($col1, $col2, $col3) = mysql_fetch_row($rslt)) { }
If I remember properly from the manual ?
-
Sep 30, 2003, 05:48 #14
- Join Date
- Aug 2003
- Location
- Singapore
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've one last query, for the code below I'm trying to evaluate whether the supplied parameter of a function exists in an array. Is this correct?
PHP Code:function getProfile ($select_team='') {
if ($select_team == (in_array($_GET['act'], $this->team))) {
//do some code
}
else {
//do some code
}
return $this->select_team['results_sql'];
}
-
Sep 30, 2003, 07:37 #15
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I just don't get your OOP -
PHP Code:$this->team
Something is way wrong with your example...
-
Sep 30, 2003, 10:28 #16
- Join Date
- Oct 2002
- Location
- Iceland
- Posts
- 1,238
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by The Red Guy
But if I understand you correctly you are checking if $select_team exists in the $this->teams array you should propably do something like
PHP Code:if (in_array($select_team, $this->team)) { }
- website
-
Oct 1, 2003, 03:25 #17
- Join Date
- Aug 2003
- Location
- Singapore
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ah thanks, but I've edited out all the variables and such, there's a var $team = array(). I would like to take the value of $select_team parameter given to the function, and check whether it is in a database, which is specified in $team[]. Thus I want to check is viewteam.php?act=teamname exists by using $_GET['act']. Next I'd automate the process by checking against whether in_array returns true, so that I can know what query to execute based on the team specified.
-
Oct 1, 2003, 07:50 #18
- Join Date
- Oct 2002
- Location
- Iceland
- Posts
- 1,238
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
so you want to check first if $_GET['act'] exists, if it exists use it, else use the $select_team var that can be passed to the function, then compare that against the array?
so something like:PHP Code:function getProfile ($select_team='') {
$team = isset($_GET['act']) ? $_GET['act'] : $select_team;
if ((in_array($team, $this->team))) {
//do some code
}
else {
//do some code
}
return $this->select_team['results_sql'];
}
- website
-
Oct 1, 2003, 22:37 #19
- Join Date
- Aug 2003
- Location
- Singapore
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks! I solved it using another method, got rid of some useless code. Once again thanks for your help!
Bookmarks