Topic: passing by reference or keyword global? (Read 736 times)
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6309
38674 credits Members referred : 374
It's time to use PHP5!
« on: Jun 26, 2006, 03: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.' ?>
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 8037
41179 credits Members referred : 3
« Reply #1 on: Jun 26, 2006, 03: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: 6309
38674 credits Members referred : 374
It's time to use PHP5!
« Reply #3 on: Jun 26, 2006, 04: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: 8037
41179 credits Members referred : 3
« Reply #6 on: Jun 26, 2006, 04: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: