Re: Using function prototypes in code [message #175038 is a reply to message #175036] |
Fri, 05 August 2011 03:11 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 8/4/2011 8:53 PM, webber wrote:
> In the interest of clarity and maintainability I would like to be able
> to write code that makes it clear what kind of arguments a function
> expects and what it returns.
>
> This is what I tried:
>
> function integer int_func(string $s) {
> // does something like, say, converting "five" to 5
> }
>
> There are two problems:
> 1 The appearance of a type name before the function name is treated as
> a syntax error
> 2 Even if I forget about declaring the return type and code it instead
> as
>
> function int_func(string $s) {
> ...
> }
>
> I get a run-time error when I call the function with a string. (eg
> $var = int_func("five");) The error message says"Catchable fatal
> error: Argument 1 passed to int_func() must be an instance of string,
> string given".
>
> It seems that basic data types cannot be specified in ths way although
> (intstances of) classes can. I have successfully used the technique to
> catch run-time errors of wrong object types when testing, but am
> surprised that I can't use it to trap unexpected basic types - or at
> least to document what is expected.
>
> To confuse me a bit further, I can't find a definitive list of the
> basic type names. For example, is it "integer" or "int"?
Plus, I should add - PHP is somewhat of a typeless language - that is,
you do not specify a type for the variable, and variables are converted
amongst the basic types as needed (pretty much, anyway). So there is no
"int", "integer", "float", etc. in the language.
You're obviously coming from a C/C++ background :) You'll find PHP to
be easier in some ways, and quite frustrating in others.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|