473,396 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing an array as a reference to a function

Noobie here (C++/C/Java experience though) ...

Recently picked up PHP ...

I want to pass an array to a function and then to use count on the
passed variable - is this the way to do it (its an object method) :

public function foo(array& $arr)
{
if (count($arr) < 1)
throw new MyException("Blah blah ...") ;
....
}
I have two questions for the pros:

1). Is the function signature correct (valid syntax?)
2). Can count accept a reference type variable ?

Oct 10 '06 #1
4 2587
En las nuevas, el Bit Byte escribió:
Noobie here (C++/C/Java experience though) ...

Recently picked up PHP ...

I want to pass an array to a function and then to use count on the
passed variable - is this the way to do it (its an object method) :

public function foo(array& $arr)
{
if (count($arr) < 1)
throw new MyException("Blah blah ...") ;
....
}
I have two questions for the pros:

1). Is the function signature correct (valid syntax?)
2). Can count accept a reference type variable ?
1) Correct syntax:

public function foo(&$arr)
{
if (count($arr) < 1)
throw new MyException("Blah blah ...") ;
....
}

2) Yes
Oct 10 '06 #2
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
Oct 10 '06 #3
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

Oct 10 '06 #4


Colin Fine wrote:
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
Thanks all. This is a very detailed (and very useful) reply.

Oct 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ohmu | last post by:
Hi! How to pass an (multidimensional)array of something to a function with reference/pointer? Can anyone help me with that? Thanks, Ohmu
15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
4
by: hello smith | last post by:
I have a lot of functions that add values to an array. They alos update a global variable of type int. Currently, I use a global variable to hold this array. All functions access this array...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.