FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Using function prototypes in code
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Using function prototypes in code [message #175036] Fri, 05 August 2011 00:53 Go to next message
webber is currently offline  webber
Messages: 1
Registered: August 2011
Karma: 0
Junior Member
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"?
Re: Using function prototypes in code [message #175037 is a reply to message #175036] Fri, 05 August 2011 03:08 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
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"?

You can only use objects or arrays with type hinting, not the standard
types (and "string" is not a defined class name). See

http://www.php.net/manual/en/language.oop5.typehinting.php

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Using function prototypes in code [message #175038 is a reply to message #175036] Fri, 05 August 2011 03:11 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
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
==================
Re: Using function prototypes in code [message #175040 is a reply to message #175036] Fri, 05 August 2011 06:35 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 05/08/2011 2:53, webber escribió/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.


PHP data types are explained at "Language Reference-> Types":

http://www.php.net/manual/en/language.types.php

And type hinting is at "Language Reference-> Classes and Objects-> type
Hinting":

http://www.php.net/manual/en/language.oop5.typehinting.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.»

function int_func(string $s) {} does not trigger a syntax error because
it's perfectly possible to do this:

class string{
}

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

In PHP you cannot define variables as integer, float or string, so there
aren't keywords as such. The closest you'll find is:

http://www.php.net/manual/en/language.types.type-juggling.php#language.type s.typecasting


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Using function prototypes in code [message #175044 is a reply to message #175036] Fri, 05 August 2011 12:52 Go to previous message
Umberto Salsi is currently offline  Umberto Salsi
Messages: 2
Registered: August 2011
Karma: 0
Junior Member
webber <frank(dot)thynne(at)gmail(dot)com> 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
> }
>


I suggest one of these

- phpDocumetor (www.phpdoc.org) generates docs from DocBlocks.

- Doxygen (www.doxygen.org) generates docs from DocBlocks.

- PHPLint (www.icosaedro/phplint) generates docs from DocBlocks, but
it is mainly a static source analyzer that implements it own parser for
the PHP language and checks if the declarations actually correspond to
the usage in the code performing a strict type check (numbers go with
numbers, strings with strings and so on). This latter is mine :-)

All these tools support a specially formatted comment named "DocBlock",
with which the function above becomes:


/**
* Short description here. Longer description follows in the
* next sentences.
* @param string $s Name of something, or similar.
* @return int Some value that tells something so and so.
*/
function int_func($s){...}



Regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: From city to lati and long
Next Topic: Regarding Session reset
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 08:54:04 GMT 2024

Total time taken to generate the page: 0.02470 seconds