tags:

views:

49

answers:

3

Example:

public function prepare($sql, &$p = NULL) {

p is assigned by reference. I'm not sure if I can assign safely NULL here as default. Is that ok in this case?

A: 

compile it and see :)

If the function can work properly with $p being NULL, then you don't have any problems.

GSto
oooh... how do you compile PHP? :)
Doug Neiner
mopoke
@mopoke, sorry I was just joking. PHP is an interpreted language and never really can be "compiled".
Doug Neiner
The script is compiled into some sort of byte code prior to execution (the byte code e.g. apc is caching).
VolkerK
@Doug: I know. As was I somewhat. But PHP *is* compiled before it gets executed. PHP 3 and below used an interpreted. PHP 4 and above are compiled.
mopoke
+3  A: 

Why would you want to have a default value for a parameter that is passed by reference? Isn't the basic point of pass-by-reference that you can change the original variable in the caller's scope? So if it has a default value, then you are not changing anything in the caller's scope, so what's the point?

newacct
The point is, that this variable is optional, isn't it? So if it is not provided, then the default value applies. Otherwise it would make no sense because the variable would always be NULL. At least that's what I think.
openfrog
+1  A: 

The code will run perfectly fine even with error_reporting set to E_ALL & E_STRICT.

And regarding the whole pass-by-reference with a default value of null, I just see it as an optional variable, and thats ok. You should definitely ask yourself whether you really need the reference because php is pretty smart about handling this type of stuff for you!

Alex