tags:

views:

73

answers:

3

Hello,

As i am reading manual of php for array functions, i see that some functions of arrays uses call by reference whereas some use call by value. Eg. array_slice uses call by value, array_splice uses call by reference. How do i remember these things without refering to the manual and thereby increase my productivity? What exact thought did the developer of php have in mind, on what basis has he made some functions made us pass values pass by value and some pass by reference? Is it done haphazardly?

Thanks in advanece :)

+5  A: 

How do i remember these things without refering to the manual and thereby increase my productivity?

I find the easiest thing is to have an IDE that can give you information about the expected parameters as you type.

Is it done haphazardly?

Probably, or through lack of foresight. Sadly, PHP's array functions (and much of the overall standard library) lack consistency in naming and parameters. This point is raised often by people criticizing PHP.

Help is on the way in the PHP 5 world, though. Check out the Array object for example, for example. It doesn't seem to cover all array functions, but as pointed out by Gordon in this comment, it's a good start.

Pekka
Thanks, i thought i am the only one who hates php :(. But i am forced to learn as it's included in curriculum.
Ankit Rathod
@Nitesh I see! Well, all in all, I love PHP, but this is arguably one of its greater weaknesses.
Pekka
The only advantage i see of PHP is it's easy to learn. But i hate that they have created 1000's of global functions. No OOP comes anywhere in picture. I also hate the fact that there is no data type and we can use variables without declaring it. It gives a huge possibility of introducing bugs.
Ankit Rathod
@Nitesh True, you can't do everything using proper OOP libraries, although that has never bothered me that much from a design point of view (as long as the own apps are nicely encapsulated, it's a minor problem IMO). Weak typing needs getting used to, too. I always say it is a good thing to learn programming with the strictness of a compiled language, and then start with PHP if one wants to. PHP is a great language to get things done, but it's a bad taskmaster for learning discipline and organization.
Pekka
@Nitesh If bugs are introduced in some code because the programmer gets confused as to what is declared or not or variable is misspelt is not the languages fault.
zaf
@zaf: Yes, but there are languages that encourage more careful/deliberate coding, and OTOH there are languages that can encourage bad habits. I would say PHP, like VB, both encourage spaghetti code. But that doesn't mean you can't write good/clean code using them.
Lèse majesté
@zaf If you have declared variable as `int color;` (may be in c# or C or C++) and by mistake if you are trying to use anything like `colour = 3;`, the compilers will always warn that such a variable is not declared whereas PHP will take everything for granted. Anyways, i don't blame PHP, it just a scripting language and not a hardcore programming language.
Ankit Rathod
+1 for Lèse majesté.
Ankit Rathod
@Lèse majesté I'm finding it hard to understand how a language can 'encourage' you to write spaghetti code. @Nitesh Thanks for that, now I know the test for a programming language to be defined as hardcore. +1 for you!
zaf
@zaf: If the standard library isn't organized in OO fashion and the language isn't designed to facilitate OOP; if there is no function-naming convention; if there is no explicit typecasting, then all of this is likely to encourage sloppy code or spaghetti code. This is a dual-edged sword since these same characteristics that make PHP so easy to pick up for beginners also means you don't need to have formal training and strong grasp of programming concepts to build a PHP application.
Lèse majesté
@Lèse majesté et al. I'll finish off with a quote from Leslie Lamport of Microsoft Research - "The typical industrial response is to provide the programmer with better debugging tools, on the theory that we can obtain good programs by putting a monkey at a keyboard and automatically finding the errors in its code."
zaf
+3  A: 

Well, to be honest, I think most people do have to reference back to the php manual from time to time. I don't think you can remember everything, and if you can, then good for you.

There is no sure way to tell (without knowing in advance) whether a function is going to take a reference or a value of, which is why you need to check the manual once in awhile.

xil3
+1  A: 

As xil3 wrote, most people do look at a reference (that's what they're for) when they code. I personally have the php.net search tool installed on Firefox. I also have a search keyword set up for it. So I can just type php array or php array_slice into the address bar and it will take me to the Arrays page and the documentation for array_slice() automatically. Aptana also gives you tooltips about each function, but they're fairly brief, out of necessity.

Generally, the functions you use regularly you will remember how to use. The ones you don't or haven't used in a while you'll just have to look up. However, as a general rule, most of the array functions that are for modifying arrays are usually passed by ref, e.g.:

array_pop()
array_push()
array_shift()
array_unshift()
array_walk()
asort()
array_multisort()

Whereas the ones that make computations using multiple arrays or are extracting something from the array generally are passed by value, e.g.:

// multiple array inputs
array_diff()
array_merge()
array_combine()
// extraction
array_values()
array_keys()
array_unique()
array_sum()

Of course, there are some that break the rules or have ambiguous names, like array_reverse(), and I often still get array_map() and array_walk() confused (the latter is by ref), but for the most part it becomes intuitive after a while.

Edit: The PHP.net search tool can be found on MyCroft. I use the one labeled "PHP Function List - en" by Lucas.

Lèse majesté