Re: Using function prototypes in code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10 Aug 2011 at 02:10, Frank Thynne <frank.thynne@xxxxxxxxx> 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.

So add the appropriate comments to your functions.

> 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".

Why are you doing this when the documentation clearly states that this is not how it works. Did you not read up about it first?

> 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.

This is PHP, not FORTRAN IV.

Personally I see it as a great step forward that for the most part, I don't have to bother.

--
Cheers  --  Tim

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux