28, May 2012

passing by reference or keyword global? - webmaster forum

 
Webdigity webmaster forums
[ 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
Instabuck - The easy way to sell digital products online

Author Topic: passing by reference or keyword global?  (Read 1660 times)
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
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 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, 02:58:20 pm by olaf »

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
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.'
?>

« Last Edit: Jun 26, 2006, 02:56:04 pm by olaf »

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Jun 26, 2006, 03: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?

Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
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: Male
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.

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
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Jun 26, 2006, 03: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?

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
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.

In oop I don't think is needed frequently.

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #7 on: Jun 26, 2006, 03: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
}

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
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....)

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
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....

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 28, 2012, 01:37:42 pm





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!






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