Re: [PHP] function by reference

2006-04-11 Thread Robert Cummings
On Tue, 2006-04-11 at 23:15, Martin Alterisio "El Hombre Gris" wrote:
> Well, it was a bad example to begin with, first of all max() is already 
> defined in php, I should at least checked that before posting. Second, I 
> forgot PHP (the Zend engine) has an interesting way of handling 
> variables, when you copy assign a var or pass it as an argument of a 
> function, a real copy is not made but a reference. When you modify the 
> reference, an actual copy is made before the modification. What's 
> happening with the function you posted is that the variables are not 
> passed by copy but by reference, that's why it works. Anyway, this is 
> due to conditions on the way PHP (the Zend engine) handles its internal, 
> you shouldn't rely on this in your code.
> 
> http://www.zend.com/zend/art/ref-count.php

More popularly known as "copy-on-write". Wikipedia does a good job of
explaining the virtues:

http://en.wikipedia.org/wiki/Copy-on-write

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] function by reference

2006-04-11 Thread Martin Alterisio \"El Hombre Gris\"
Well, it was a bad example to begin with, first of all max() is already 
defined in php, I should at least checked that before posting. Second, I 
forgot PHP (the Zend engine) has an interesting way of handling 
variables, when you copy assign a var or pass it as an argument of a 
function, a real copy is not made but a reference. When you modify the 
reference, an actual copy is made before the modification. What's 
happening with the function you posted is that the variables are not 
passed by copy but by reference, that's why it works. Anyway, this is 
due to conditions on the way PHP (the Zend engine) handles its internal, 
you shouldn't rely on this in your code.


http://www.zend.com/zend/art/ref-count.php

tedd wrote:

The ampersand before the function name indicates that the function 
returns a reference instead of a copy of the variable, for example:


 $var2) return $var1;
else return $var2;
  }

  $global1 = 10;
  $global2 = 9;
  $maxglobal =& max($global1, $global2);
  $maxglobal++;
  echo $global1;
  //this will print 11 since $maxglobal is a reference to $global1
?>



It's going to hurt thinking about that.  :-)

But that produces the same results as:

 $var2) return $var1;
else return $var2;
  }

  $global1 = 10;
  $global2 = 9;
  $maxglobal =& max($global1, $global2);
  $maxglobal++;
  echo $global1;
  //this will print 11 since $maxglobal is a reference to $global1
?>

Note the absence ampersands in the function, but an ampersand remains 
in the assignment:


Thanks.

tedd


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] function by reference

2006-04-11 Thread Chris

tedd wrote:
The ampersand before the function name indicates that the function 
returns a reference instead of a copy of the variable, for example:


 $var2) return $var1;
else return $var2;
  }

  $global1 = 10;
  $global2 = 9;
  $maxglobal =& max($global1, $global2);
  $maxglobal++;
  echo $global1;
  //this will print 11 since $maxglobal is a reference to $global1
?>



It's going to hurt thinking about that.  :-)


References (and pointers in lower level languages) can be painful :(

It's quite useful with objects.

function &getdb() {
  if (!isset($GLOBALS['DBClass'])) {
$dbclass = &new DBClass();
.
$GLOBALS['DBClass'] = &$dbclass;
  }
   return &$GLOBALS['DBClass'];
}

So in this case, every time you call 'getdb', you don't want it to start 
a new object and return that (memory usage because it has to set up the 
object again, you have to reconnect to the db and so on).


You want it to use the same object over and over again.

So you return by reference.


You also have to call by reference:

$my_db = &getdb();

rather than:

$my_db = getdb();

otherwise it returns a copy rather than a reference.

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] function by reference

2006-04-11 Thread tedd
The ampersand before the function name indicates that the function 
returns a reference instead of a copy of the variable, for example:


 $var2) return $var1;
else return $var2;
  }

  $global1 = 10;
  $global2 = 9;
  $maxglobal =& max($global1, $global2);
  $maxglobal++;
  echo $global1;
  //this will print 11 since $maxglobal is a reference to $global1
?>


It's going to hurt thinking about that.  :-)

But that produces the same results as:

 $var2) return $var1;
else return $var2;
  }

  $global1 = 10;
  $global2 = 9;
  $maxglobal =& max($global1, $global2);
  $maxglobal++;
  echo $global1;
  //this will print 11 since $maxglobal is a reference to $global1
?>

Note the absence ampersands in the function, but an ampersand remains 
in the assignment:


Thanks.

tedd
--

http://sperling.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] function by reference

2006-04-11 Thread Richard Lynch
On Tue, April 11, 2006 10:42 am, tedd wrote:
>function &ref3($a)
>   {
>   $a--;
>   }
>
> ?>

I believe this one will only "matter" if you are RETURNING a value.

And only really matter if that value is an object...

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] function by reference

2006-04-11 Thread Martin Alterisio
The ampersand before the function name indicates that the function returns a
reference instead of a copy of the variable, for example:

 $var2) return $var1;
else return $var2;
  }

  $global1 = 10;
  $global2 = 9;
  $maxglobal =& max($global1, $global2);
  $maxglobal++;
  echo $global1;
  //this will print 11 since $maxglobal is a reference to $global1
?>

2006/4/11, tedd <[EMAIL PROTECTED]>:
>
>
> Additionally, what I don't get is this:
>
>  $a = 10;
> echo("$a ");
> ref1(&$a);
> echo("$a ");
>
> $a = 10;
> echo("$a ");
> ref2($a);
> echo("$a ");
>
> $a = 10;
> echo("$a ");
> ref3($a);
> echo("$a ");
>
> ?>
>
>
>  function ref1($a)
> {
> $a--;
> }
>
> ?>
>
>  function ref2(&$a)
> {
> $a--;
> }
>
> ?>
>
>  function &ref3($a)
> {
> $a--;
> }
>
> ?>
>
> The first two functions work as I would expect. The last one is shown
> in the documentation, but doesn't work as I expected. There doesn't
> appear to be any difference between ref1 and ref3 -- so what's with
> the "ampersand" in &ref3? What is an ampersand supposed to mean/do
> when it precedes a function?
>
> Thanks.
>
> tedd
> --
>
> 
> http://sperling.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Re: [PHP] function by reference

2006-04-11 Thread tedd

At 7:31 PM -0700 4/10/06, Ray Hauge wrote:

 >

 I believe you're thinking more of the C++ style of returning by reference.
 This article should help explain how references work, and how to return
 references... most specifically check the "Returning References" section.


 > http://www.php.net/manual/en/language.references.php

nevermind... I what I read and what was actually written seem to have been two
entirely different things ;)  Still good info though.

--
Ray Hauge


Ray:

It was an interesting read -- but, I found it, and the comments, confusing.

The difference I was able to get was I know if you display a C 
pointer, would will see a memory address -- if you display a PHP 
reference, you will see the value. As such, one is a memory address 
and the other is a symbol table alias -- but they still seem to work 
the same. I haven't seen a working difference yet.


Additionally, what I don't get is this:

");
ref1(&$a);
echo("$a ");

$a = 10;
echo("$a ");
ref2($a);
echo("$a ");

$a = 10;
echo("$a ");
ref3($a);
echo("$a ");

?>








The first two functions work as I would expect. The last one is shown 
in the documentation, but doesn't work as I expected. There doesn't 
appear to be any difference between ref1 and ref3 -- so what's with 
the "ampersand" in &ref3? What is an ampersand supposed to mean/do 
when it precedes a function?


Thanks.

tedd
--

http://sperling.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] function by reference

2006-04-10 Thread Ray Hauge
>
> I believe you're thinking more of the C++ style of returning by reference.
> This article should help explain how references work, and how to return
> references... most specifically check the "Returning References" section.
>
> http://www.php.net/manual/en/language.references.php
>
> HTH
> --
> Ray Hauge
> Programmer/Systems Administrator
> American Student Loan Services
> www.americanstudentloan.com
> 1.800.575.1099

nevermind... I what I read and what was actually written seem to have been two 
entirely different things ;)  Still good info though.

-- 
Ray Hauge
Programmer/Systems Administrator
American Student Loan Services
www.americanstudentloan.com
1.800.575.1099

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] function by reference

2006-04-10 Thread Ray Hauge
On Monday 10 April 2006 19:19, Richard Lynch wrote:
> On Sun, April 9, 2006 1:17 pm, tedd wrote:
> > Hi gang:
> >
> > Not that I have an immediate need for this, but in other languages
> > one can access a function by reference (in other words, it's address
> > -- such as call(function address) ).
> >
> > In php, one can pass a variable by reference by simply using the
> > ampersand, such a &$a.
> >
> > Is there a similar way to reference a function?
> >
> > Rob, was kind enough to post the following code, but I was looking
> > for something where I could store the function's address in a
> > variable. Something like:
> >
> > $a = &f_a();
> >
> > And then, where I could use call_user_func($a); (or something
> > similar) and the function would do it's thing -- anything like that?
> >
> > Thanks
> >
> > tedd
> >
> > --- Rob's suggestion follows.
> >
> > Like the following?
> >
> >  >
> > function f_a()
> > {
> >  echo 'a';
> > }
> >
> > function f_b()
> > {
> >  echo 'b';
> > }
> >
> > function f_c()
> > {
> >  echo 'c';
> > }
> >
> > $map = array
> > (
> >  'a' => 'f_a',
> >  'b' => 'f_b',
> >  'c' => 'f_c',
> > );
> >
> > $map['a']();
> > $map['b']();
> > $map['c']();
> >
> > ?>
> >
> >
> > --
> > -
> >--- http://sperling.com
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
>
> --
> Like Music?
> http://l-i-e.com/artists.htm

I believe you're thinking more of the C++ style of returning by reference.  
This article should help explain how references work, and how to return 
references... most specifically check the "Returning References" section.

http://www.php.net/manual/en/language.references.php

HTH
-- 
Ray Hauge
Programmer/Systems Administrator
American Student Loan Services
www.americanstudentloan.com
1.800.575.1099

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] function by reference

2006-04-10 Thread Richard Lynch


On Sun, April 9, 2006 1:17 pm, tedd wrote:
> Hi gang:
>
> Not that I have an immediate need for this, but in other languages
> one can access a function by reference (in other words, it's address
> -- such as call(function address) ).
>
> In php, one can pass a variable by reference by simply using the
> ampersand, such a &$a.
>
> Is there a similar way to reference a function?
>
> Rob, was kind enough to post the following code, but I was looking
> for something where I could store the function's address in a
> variable. Something like:
>
> $a = &f_a();
>
> And then, where I could use call_user_func($a); (or something
> similar) and the function would do it's thing -- anything like that?
>
> Thanks
>
> tedd
>
> --- Rob's suggestion follows.
>
> Like the following?
>
> 
> function f_a()
> {
>  echo 'a';
> }
>
> function f_b()
> {
>  echo 'b';
> }
>
> function f_c()
> {
>  echo 'c';
> }
>
> $map = array
> (
>  'a' => 'f_a',
>  'b' => 'f_b',
>  'c' => 'f_c',
> );
>
> $map['a']();
> $map['b']();
> $map['c']();
>
> ?>
>
>
> --
> 
> http://sperling.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] function by reference

2006-04-10 Thread tedd



 > Boy, am I in love with this language -- it gives you plenty of

 shovels to dig yourself in as deep as you want.


*lol*. personally I prefer the other format which was what I tried to
illustrate with my original example:


Oh, you illustrated it very well. I looked intently at it and learned 
from it. I'm just into "one focus" when I ask those type of 
questions. I had something very specific in mind and that last post 
you may hit the mark!


Thanks again.

tedd

--

http://sperling.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] function by reference

2006-04-10 Thread Robert Cummings
On Mon, 2006-04-10 at 09:42, tedd wrote:
> >It's the same thing for the most part...
> >
> > >
> >$a = 'f_a';
> >call_user_func( $a, $p1, $p2, $p3 )
> >
> >?>
> >
> >Cheers,
> >Rob.
> 
> Rob:
> 
> No way dude -- that was too easy!
> 
> Boy, am I in love with this language -- it gives you plenty of 
> shovels to dig yourself in as deep as you want.

*lol*. personally I prefer the other format which was what I tried to
illustrate with my original example:



All it means, is that once you have the name of a function in a variable
you can invoke the variable as a function and it will invoke the
function matching the variable's value. Some of this stuff can be really
useful for doing backwards compatibility with older versions of PHP:



Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] function by reference

2006-04-10 Thread tedd

It's the same thing for the most part...



Cheers,
Rob.


Rob:

No way dude -- that was too easy!

Boy, am I in love with this language -- it gives you plenty of 
shovels to dig yourself in as deep as you want.


Thanks Rob.

tedd
--

http://sperling.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] function by reference

2006-04-09 Thread Robert Cummings
On Sun, 2006-04-09 at 14:17, tedd wrote:
> Hi gang:
> 
> Not that I have an immediate need for this, but in other languages 
> one can access a function by reference (in other words, it's address 
> -- such as call(function address) ).
> 
> In php, one can pass a variable by reference by simply using the 
> ampersand, such a &$a.
> 
> Is there a similar way to reference a function?
> 
> Rob, was kind enough to post the following code, but I was looking 
> for something where I could store the function's address in a 
> variable. Something like:
> 
> $a = &f_a();
> 
> And then, where I could use call_user_func($a); (or something 
> similar) and the function would do it's thing -- anything like that?

It's the same thing for the most part...



Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Function by reference?

2001-01-31 Thread Wico de Leeuw

At 11:35 31-1-01 +0300, Max A. Derkachev wrote:
>Hello Niklas,
>
>Wednesday, January 31, 2001, 7:02:49 PM, you wrote:
>NS> 
>$tmpParsing=eregi_replace("href=(\")([^*]*)(\")","href=\"\\1\"",$this->dataToBeParsed);

Take a look @ Preg_Replace with the e modifier, i think you are looking for 
that (php4 only)

Greetz,

Wico


>NS> where I want \\1 to be fed into urlencode()  I was thinking somewhere 
>along the lines
>
>NS> $func=&urlencode;
>
>It won't work. You can not reference a function. But you can call a
>variable with the function name value.
>$func = 'urlencode'
>$func($str) here will do the same as urlencode($str).
>
>--
>Best regards,
>Max A. Derkachev mailto:[EMAIL PROTECTED]
>Symbol-Plus Publishing Ltd.
>phone: +7 (812) 324-53-53
>http://www.Books.Ru -- All Books of Russia
>
>
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>To contact the list administrators, e-mail: [EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Function by reference?

2001-01-31 Thread Max A. Derkachev

Hello Niklas,

Wednesday, January 31, 2001, 7:02:49 PM, you wrote:
NS> 
$tmpParsing=eregi_replace("href=(\")([^*]*)(\")","href=\"\\1\"",$this->dataToBeParsed);

NS> where I want \\1 to be fed into urlencode()  I was thinking somewhere along the 
lines

NS> $func=&urlencode;

It won't work. You can not reference a function. But you can call a
variable with the function name value.
$func = 'urlencode'
$func($str) here will do the same as urlencode($str).

-- 
Best regards,
Max A. Derkachev mailto:[EMAIL PROTECTED]
Symbol-Plus Publishing Ltd.
phone: +7 (812) 324-53-53
http://www.Books.Ru -- All Books of Russia
 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]