Topic: passing by reference or keyword global? (Read 1660 times)
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« on: Jun 26, 2006, 02:48:38 pm »
Hello,
just "stumbled" this way of function parameter passing inside the PHP manual (yes, there are still things I never used in php )
Code:
<?php function add_some_extra(&$string) { $string .= 'and something extra.'; } $str = 'This is a string, '; add_some_extra($str); echo $str; // outputs 'This is a string, and something extra.' ?>
I used this kind of code to get the value inside a faunction:
Code:
<?php function add_some_extra() { global $string; $string .= 'and something extra.'; } $string = 'This is a string, '; add_some_extra(); echo $string; // outputs 'This is a string, and something extra.' ?>
which is the better way?
EDIT: fixed the second example...
« Last Edit: Jun 26, 2006, 02:58:20 pm by olaf »
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 5799
46391 credits Members referred : 3
« Reply #1 on: Jun 26, 2006, 02:51:54 pm »
Both ways do the exact same thing.
The first is good when you work with objects, as the second wont work inside of an object.
yes, right inside objects I use "$this" to access variables. What you're syaing is about this?
Code:
<?php class foo() { function add_some_extra(&$string) { $string .= 'and something extra.'; } } $str = 'This is a string, '; $obj = new foo; $obj->add_some_extra($str); echo $str; // outputs 'This is a string, and something extra.' ?>
<?php class foo() { function add_some_extra(&$string) { $string .= 'and something extra.'; } } $str = 'This is a string, '; $obj = new foo; $obj->add_some_extra($str); echo $str; // outputs 'This is a string, and something extra.' ?>
there went something wrong while posting the last answer... this should be an example for "passing function paramaters" within an object, right?
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #3 on: Jun 26, 2006, 03:02:46 pm »
this is the good version:
Code:
<?php class foo { function add_some_extra(&$string) { $string .= 'and something extra.'; } } $str = 'This is a string, '; $obj = new foo; $obj->add_some_extra($str); echo $str; // outputs 'This is a string, and something extra.' ?>
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 5799
46391 credits Members referred : 3
« Reply #4 on: Jun 26, 2006, 03:06:30 pm »
That is correct, but I think that in classes you should not use this kind of programming.
I mean if a variable holds something that should be passed by reference to the class then propably it should be a property of the class.
$obj = new foo; echo $obj->add_some_extra('This is a string, ');
but what is a real world example for passing by reference?
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 5799
46391 credits Members referred : 3
« Reply #6 on: Jun 26, 2006, 03:15:22 pm »
Quote
but what is a real world example for passing by reference?
It is usefull when you want to keep arrays-collections of objects without using a lot of memory (with the clone command) or when you are using procedural programming.
but what is a real world example for passing by reference?
It is usefull when you want to keep arrays-collections of objects without using a lot of memory (with the clone command) or when you are using procedural programming.
In oop I don't think is needed frequently.
strange, while reading the iterator article I found passing by reference inside classes like:
Code:
<?php function & foo($val) { //code here }
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 5799
46391 credits Members referred : 3
« Reply #8 on: Jun 26, 2006, 03:24:48 pm »
I haven't seen that before, but maybe is the same thing with foo(&$val, &$val2, etc....)