php-general Digest 10 Aug 2011 18:34:59 -0000 Issue 7438

Topics (messages 314459 through 314463):

Re: Using function prototypes in code
        314459 by: Stuart Dallas
        314460 by: Tim Streater
        314461 by: David Harkness
        314462 by: Simon J Welsh

text insertion
        314463 by: Chris Stinemetz

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On 10 Aug 2011, at 02:10, Frank Thynne 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"?

The manual says...

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

http://php.net/language.oop5.typehinting

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

--- End Message ---
--- Begin Message ---
On 10 Aug 2011 at 02:10, Frank Thynne <frank.thy...@gmail.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.

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

--- End Message ---
--- Begin Message ---
On Tue, Aug 9, 2011 at 6:10 PM, Frank Thynne <frank.thy...@gmail.com> wrote:

> function integer int_func(string $s) {
>  // does something like, say, converting "five" to 5
> }
>

As Stuart pointed out, type-hinting currently only works for classes and
arrays. Scalar type-hinting is planned for the future, but for now you're
left with enforcing it in the function itself. You can create a class with
static helpers to make this easier if you're going to do it frequently.

As for documentation, go with the PHPDoc standard:

/**
 * Converts a spelled-out number to the equivalent integer.
 *
 * @param string $s must be a spelled out number, e.g. "five" rather than
"5".
 * @return int
 */
function int_func($s) { ... }

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


I use "int" and "bool" with the rest spelled out which works with casting.

Peace,
David

--- End Message ---
--- Begin Message ---
On 10/08/2011, at 1:10 PM, Frank Thynne wrote:

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

Both. 
http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting
---
Simon Welsh
Admin of http://simon.geek.nz/


--- End Message ---
--- Begin Message ---
How do I preserve text formatting when text is inserted into a database table?

For example: I am using a textarea to allow users to leave comments
into the database and I am using:

'" . mysql_real_escape_string($_POST['store_comments']) . "',   to
prevent SQL injection;

But when I call the data with php from the database the format it was
inserted is not preserved. Is there a way to make sure when a user
adds new paragraphs or indentation it will be preserved?

Thank you,

Chris

--- End Message ---

Reply via email to