when using arrays as parameters in functions, should I pass them by reference or by value? Or there is not that much significance?
Normally you should pass by value.
You should only pass be reference if you need to modify the array in place, rather than returning a new one.
There's no performance benefit to passing by reference when reading from an array because PHP uses copy-on-write,
If you're interested in maximising performance, do a speed test. Due to PHP optimisations, passing by reference is usually faster than passing by value only if you modify the array (however note the behaviour is also different!).
Passing by value helps reduce the side-effects of a function (side-effects are a common source of code complexity and bugs) and for this and other reasins, it is "better style".
So, pass by reference only if there is a good reason to do so.
Related
I have written a file-handler-class, that works like this:
__construct opens and ex-locks a file, reads its json-content and parses that as an PHP-array, keeping this as a property of the class.
The file is still locked, in order to avoid race-conditions.
Other 'worker-classes' make changes in this Array, in/from other scopes.
__destruct encodes the finished Array, writes it to file, and unlocks the file.
Everything works fine ...
QUESTION:
Is it sensible to keep the Array as a property of the original class, or is it better to pass the Array to the worker-classes, and let them return it at the end?
Perhaps there is a way to keep the Array locally, and pass it to worker-classes by reference, instead of as raw data?
I mean ... this is a question of not having duplicates, waisting memory. A question of speed, not passing things unnecessarily. And a question of best practices, keeping things easy to understand.
Actually, by passing the array to another function, having that function modify the array, and then return it to some other caller that may or may not also conduct modifications on it, you are in fact copying that array multiple times (since this invokes copy-on-write semantics in PHP) and by definition wasting memory.
Whereas by keeping it as a property of the object instance, you would not be invoking any copy-on-write semantics, even if the caller is not the same instance. Since passing an object instance won't copy the array, nor will its modification from said instance.
Not to mention you just make it easier to retain state within that object (assuming you care about validation).
I may sound stupid to some but I want to know if there is some benefit of passing arguments to a function as an array,rather than passing each arguments or some downsides?
Unwritten rule for good programming practice is that function should not have more than 3-5 arguments.
Usually arrays or even objects are used to pass in the logical complete data structures.
I think this is due more transparent and readable code rather than any performance benefits.
Sure it might look nicer if you pass an array to a method, but what does it mean?
Arrays usually signify that you have a collection of the same thing, whilst method parameters are usually different things.
If you want to pass a list of things to a method and do the same action on all of them, then it makes perfect sense to use some type of array/collection object.
If however you want to make it tidier and avoid passing around lots of objects together, consider refactoring your code to use some kind of wrapper object that you can pass around more easily.
Also if you have so many arguments that you would consider using an array to hold them, it's a sure sign that you need to refactor your code ;-)
Mostly it just requires more typing to create an array and pass it, rather than just passing individual arguments.
Other than that there's no particular specific advantage to separate parameters.
Parameter list much more clear when you read function signature. Array is just one variable, say, $args. But what there in args?
if you have this array already, it is surely better to use it.
if you don't have this array already, using parameters will save you typing of array keyword and a couple of braces.
that's all.
Use whatever you feel more suitable for the case.
I think that when you have a few parameters or less than 5 (subjectively) then more useful is passing arguments as parameters. If you have a big count of arguments then using array is more useful that function/method with 15 parameters.
Well, I read in my handy PHP book that it's very important to be able to distinguish between reference and variable parameters. The book says that the original value of parameterized variables are preserved when the variable is changed, and the original values of parameterized references change when the reference is changed. It says that's the key difference, if I am reading right.
Well, I'm wondering when each is more useful than the other. How do I know when to use variables and when to use references when I create my own functions?
It's pretty straightforward. Use references when you need to modify the value of the variable passed in to the function. Use variables when you don't need to or want to modify the value.
So, for example, if you're writing a function that takes an array and changes that array, you'd be better off using a reference for that array rather than returning a new array from the function.
"References" (variable aliases) make your code harder to understand and could be a source of hard to follow errors. There are no valid reasons to use references in php and to be on the safer side try to avoid them altogether.
And no, objects in php5 have nothing to do with "references".
"References" as implemented in php is a strange concept. Normally, in programming languages variables are independent of each other so that changing one variable doesn't affect others. Php "references" allow several variables to share the same value and to be dependent of each other. Basically, you change one variable, and suddenly another one, which you think is totally unrelated, is getting changed too. It's no good thing and often leads to much confusion.
Objects in php (do I need to add 'five'?) have nothing to do with "references" in the above sense. They behave much like C pointers (actually, this is what they are under the hood) - when you pass an object to a function, you actually pass a pointer, and the function can use this pointer to manipulate the object contents, but there's no way for the function to change the passed variable itself, for example, make it point to another object.
This "objects are references" misunderstanding is probably because people confuse php "references" (ampersand syntax) with the generic CS term , which also applies to pointers, handles etc.
Is it possible to omit certain variables from serialization? Say I have a temporary variable in a php object that I don't want serialized as it is a waste of space. The only thing I can think of is making them static but this is not ideal as it is not really part of the object which there will be many instances of.
This may not even be possible but would love to hear some ideas.
Take advantage of the __sleep method of your object.
In PHP, I'm frequently doing lots of string manipulation. Is it alright to split my code into multiple functions, because if primitive types like strings are passed by value I would be significantly affecting performance.
Only objects are passed by reference.
That doesn't mean you'll get a performance boost by changing to references though - PHP uses copy-on-write, so a copy is only made if you modify the variable.
Splitting your code into functions won't slow it down from that point of view.
There is a small overhead for calling a function, but unless your in a loop calling 10,000s of them it's probably not something you need to worry about.
Objects are passed by reference. Everything else is passed by value unless you explicitly use pass-by-reference with the & operator.
That being said, PHP also uses copy-on-write to avoid unnecessary copying.
Yes, primitives are passed by value unless you explicitly define the function to pass by reference (by using an ampersand & in front of the parameter) or invoke the function with an ampersand in front of the argument. (The latter of which is deprecated)
See this part of the documentation for more.
EDIT
Also, the statement that "objects are passed by reference" in PHP is a bit of a simplification, though it can often be thought of that way for most purposes. This chapter of the documentation explains the differences.
Passing by reference is actually slower than passing by value in PHP. I can't find the correct citation for this claim; it's somewhere in the "References" section of the PHP manual.
By default, everything is passed by value. If you want to pass something by reference you have to explicitly state it as so.
Here is the php documentation that explicitly states this behavior.