Reputation: 16910
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 referring 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?
Upvotes: 1
Views: 126
Reputation: 8045
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.
Upvotes: 1
Reputation: 449733
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.
Upvotes: 5
Reputation: 16439
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.
Upvotes: 3