Page 1 of 1
function setting argument type problem
Posted: Wed Sep 02, 2009 10:57 am
by ctrLogicDotNet
Something must be escaping my mind...
I would like the arguments of a function to be type specific...
Code: Select all
public function myFunc(array $array, bool $bool=null){...
I can't seem to be able to default the value to the type specified, as to be null.
Also when I call the function like this:
I'm getting this error:
Code: Select all
Catchable fatal error: Argument 2 passed to myFunc() must be an instance of bool, boolean given
Anyone can clarify this for me?
Re: function setting argument type problem
Posted: Wed Sep 02, 2009 11:01 am
by jazz090
well that's easy: a boolean is either true or false and assigning null to a boolean will create a paradox in your code.
depending on what you need, most programmers set the default to FALSE.
Re: function setting argument type problem
Posted: Wed Sep 02, 2009 11:04 am
by ctrLogicDotNet
This is what I got as an error when trying to set it like you said
Code: Select all
Fatal error: Default value for parameters with a class type hint can only be NULL ...
I set it this way:
Code: Select all
public function myFunc(array $array, bool $bool=TRUE){...
Re: function setting argument type problem
Posted: Wed Sep 02, 2009 11:12 am
by jazz090
threes something wrong with your code because its working in my code. if its giving you a hard time, just remove the var hint and it will work.
Re: function setting argument type problem
Posted: Wed Sep 02, 2009 11:17 am
by ctrLogicDotNet
well I don't know what to say, the only code I implemented is the one stated above...
and I get this error...
Re: function setting argument type problem
Posted: Wed Sep 02, 2009 11:23 am
by ctrLogicDotNet
From
http://us2.php.net/manual/en/language.o ... inting.php
Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported.
???
Re: function setting argument type problem
Posted: Wed Sep 02, 2009 11:40 am
by Ollie Saunders
You can only type hint for classes, interfaces, or array (a special case). When you say:
Code: Select all
function foo(bool $aBoolean) { ...
You're saying you'll only accept a
class or interface called bool not the scalar bool.
Better still, don't type hint. It won't make your programs more error-resilient only less flexible. Listen to Jenk in
this thread, he's a smart guy.
Re: function setting argument type problem
Posted: Wed Sep 02, 2009 12:04 pm
by ctrLogicDotNet
Thanks for your answer Ollie Saunders...
Thought that specifying the type would prevent me of testing it inside the function
And this way specifying to the user the misuses of a function by providing wrong type of arguments...
Re: function setting argument type problem
Posted: Wed Sep 02, 2009 12:16 pm
by Ollie Saunders
ctrLogicDotNet wrote:Thought that specifying the type would prevent me of testing it inside the function
Well, it won't. Not if you're testing for boolean. The only way reasonable way you can do that is with is_bool().
And this way specifying to the user the misuses of a function by providing wrong type of arguments...
That's not a good reason, believe me. Do not write your code expressly to prevent you or other programmers from doing a bad job, not in PHP at least; such things should be implicit from your design. Others may oppose this viewpoint, but they will be wrong. Instead, make the purpose of your parameter obvious with a good name, if you can't do that, there's possibly something wrong with the design that deemed this function/parameter to be necessary.