13, May 2008

passing by reference or keyword global? - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: passing by reference or keyword global?
« previous next »
Pages: [1] Print

Author Topic: passing by reference or keyword global?  (Read 682 times)
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6236
38242 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 Wink)

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, 03:58:20 PM by olaf »


Last blog : Database Management with phpMyAdmin
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7823
39873 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.'
?>

« Last Edit: Jun 26, 2006, 03:56:04 PM by olaf »

Trial and Error my two best teachers Cool
Promote your blog for free.... Visit through proxy

Last blog : Keep it Legal - Tims guide to legal notices
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6236
38242 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Jun 26, 2006, 04:01:11 PM »

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.'
?>


there went something wrong while posting the last answer... Cheesy
this should be an example for "passing function paramaters" within an object, right?


Last blog : Database Management with phpMyAdmin
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6236
38242 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.'
?>


Last blog : Database Management with phpMyAdmin
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7823
39873 credits
Members referred : 3



« Reply #4 on: Jun 26, 2006, 04: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.

eg :

Code:
<?php

class foo {
var 
$string;

function 
add_some_extra($string) {
   
$this->string .= $string 'and something extra.';
}
}

$obj = new foo;
$obj->add_some_extra('This is a string, ');
echo 
$this->string

Trial and Error my two best teachers Cool
Promote your blog for free.... Visit through proxy

Last blog : Keep it Legal - Tims guide to legal notices
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6236
38242 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Jun 26, 2006, 04:11:09 PM »

Yes, thats the way I use with classes but more like this way:

Code:
<?php

class foo {


function 
add_some_extra($string) {
   return .= 
$string 'and something extra.';
}
}

$obj = new foo;
echo 
$obj->add_some_extra('This is a string, ');
Wink

but what is a real world example for passing by reference?


Last blog : Database Management with phpMyAdmin
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7823
39873 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.

In oop I don't think is needed frequently.

Trial and Error my two best teachers Cool
Promote your blog for free.... Visit through proxy

Last blog : Keep it Legal - Tims guide to legal notices
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6236
38242 credits
Members referred : 374


It's time to use PHP5!


« Reply #7 on: Jun 26, 2006, 04:18:28 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.

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
}


Last blog : Database Management with phpMyAdmin
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7823
39873 credits
Members referred : 3



« Reply #8 on: Jun 26, 2006, 04:24:48 PM »

I haven't seen that before, but maybe is the same thing with foo(&$val, &$val2, etc....)

Trial and Error my two best teachers Cool
Promote your blog for free.... Visit through proxy

Last blog : Keep it Legal - Tims guide to legal notices
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=3031
Tags : php articles programming oop Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: passing by reference or keyword global?
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 13, 2008, 01:18:20 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 34.929
Total Topics: 7.262
Total Members: 3.479
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: mileymo1

22 Guests, 3 Users online :

12 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.