tags:

views:

62

answers:

6

What could be the reason for having this:

public function __construct($host, $port, $timeout = 5){
    $errnum = 0;
    $errstr = ''; 

Instead of this:

public function __construct($host, $port, $errnum = 0, $errstr = '', $timeout = 5){

?

Why some are params and others aren't ?

Thanks a lot, MEM

+1  A: 

If they would be params, the user could pass them in during the creation of the object. A call like

$a = new MyObject($myhost, $myport, 40000, 'Failed.', $mytimeout);

would initialize your object with an error already in its memory... In the case of an error number or string, that is rather unwanted. The user shouldn't be able to poke a random error into your object.

MvanGeest
Yup. Thanks. :D
MEM
+2  A: 

the errors are set by the function, and there is no point passing those in

second
Can you please elaborate?
MEM
I see now. Thanks. :)
MEM
+5  A: 

A function definition defines a contract between the function itself and the code that calls it.

A variable should only be a parameter if the caller should specify it's value. Otherwise if a variable is only used internally by the function, there is no need to specify it as a parameter.

Justin Ethier
Thank you. Can I please ask a related question: I often see $errnum and $errstrWhy not other names?Thanks a lot:)
MEM
@MEM Those are just conventions. You could name them whatever you want, but the name should make sense. Those names likely appear in a lot of examples in the php manual.
George Marian
Thank you. We have a convention, or at least I follow this what, that is to name $variablesLikeSo "lower camel case" (I suppose we call it), and then I find myself into a conflit when I see so often those vars... I believe I will change, I always make then quite clear like $errorNumber $errorDescription so, I believe it will be ok yes?Thank you again.
MEM
+1  A: 

Normally you define a function in a way that it only accepts parameters/data that it definitely needs in order to run.

In you example, $errnum and $errstr seem to be variables that the function uses internally. If you design that function you have to decide whether you want to give the user the possibility to override those or not.

Felix Kling
Quite clear. :) Thank you. Again. :)
MEM
1 question if you don't mind, so optional params... are not something to considering on a method parameters ?
MEM
@MEM: They can be used, but only if they make sense. Again: If you don't want the user to modify something, don't give him the opportunity.
Felix Kling
I see... I will think of that next time and try to understand if the option that I give to the user to either use a param or not is not something that, at the end, will be something that I can re-arrange with another structure, living the decision to the system instead.
MEM
A: 

Maybe you want to call the constructor with more than 3 parameters, depending on what the constructor/class do. The parameter list is not the place to initialize local variables. Check the API of the class you are reading what the parameters are for (looks like they are for the fsockopen function, so read this documentation first).

Progman
;) Thank you. It was a more basic ignorance of me then, a way to understand how this class works. (that's actually a class that I'm building with help, and so, that was the reason for this question. Thank you. :)
MEM
A: 

$timeout is a default parameter that can be overwritten when calling the function.

$errnum and $errstr cannot be overwritten when calling the function.

Observe:

public function goodConstruct($host, $port, $timeout = 5){
    $errnum = 0;
    $errstr = ''; 
}

goodConstruct('hostname',8443,60);

By doing this, I can overwrite the default timeout.

public function badConstruct($host, $port, $errnum = 0, $errstr = '', $timeout = 5)
{

//code
}

badConstruct('hostname',8443,99,'hey look at this silly error!!!!',900);

Now I can also overwrite the error code (assuming that's the purpose of errnum, it's even worse if that's some sort of a counter) and the error string. Do you really want to be able to control this from your function call? Probably not... I assume that you'd want that to be fixed.

baultista
I see. Thanks! It was an excellent add on. I've not thinking the relation between arguments and the overwritten. Nice. :)
MEM