You are here: Re: Passing an array as a reference to a function « PHP Programming Language « IT news, forums, messages
Re: Passing an array as a reference to a function

Posted by Colin Fine on 10/10/06 19:02

Robin wrote:
> Bit Byte wrote:
>> 1). Is the function signature correct (valid syntax?)
>
> Depends on your PHP-Version, i.e.:
>
> PHP 4:
> function foo(&$arr){}
>
> PHP 4 knows nothing about visibilty and always passes by value, hence
> the '&' to signal a pass-by-reference. Nice text about using objects
> and references in PHP 4:
> http://www.obdev.at/developers/articles/00002.html
>
> PHP 5:
> public function foo(array $arr){}
>
> PHP 5 always passes by reference, so no '&' is needed.
>
> Cheers
> Robin

I don't believe this is correct.

http://www.php.net/manual/en/functions.arguments.php says:

"By default, function arguments are passed by value (so that if you
change the value of the argument within the function, it does not get
changed outside of the function). If you wish to allow a function to
modify its arguments, you must pass them by reference.

"If you want an argument to a function to always be passed by reference,
you can prepend an ampersand (&) to the argument name in the function
definition:"

I think that what you are thinking of is that in PHP5 *objects* are
always passed by reference, but arrays are not objects.

Therefore the & *is* still needed in PHP5.

Furthermore, I can't find where it says so, but I'm pretty sure that the
PHP5 extension that allows you to specify a type for a function argument
is limited to objects.

If I am right, then :

for an array in PHP5

public function foo(&$arr) {

but if you use an object class MyArray

public function foo (MyArray $arr) {

Colin

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация