8

i have this function:

function myFunction(string $name) {
    $db = mysql_connect("localhost", "root", "");
    mysql_select_db(...); 

    $insertplayer="INSERT INTO `...`(....)
    VALUES (......')";
    if (!mysql_query($insertplayer,$db))
      {
      die('Error: ' . mysql_error());
      }
    $id = mysql_insert_id();
    echo 'done for player N°'.$id;
    mysql_close($db);
}

and the form i use:

<form action="insertplayer.php" method="post">
    <input type="text" name="nameplayer" />
    <input type="submit" value="ok" />
</form>

But when i do this, i have this error:

Catchable fatal error: Argument 1 passed to myFunction() must be an instance of string, string given, called in C:.... on line 23 and defined in C:...

I have this error with the int problem. How can i resolved it ?

6
  • 1
    you don't need to use dataType string in php in your function Commented Mar 13, 2014 at 16:18
  • 6
    Type hinting is not for primative types, php is looking for a string class that does not exist. Remove that type hint: myFunction($name) { Commented Mar 13, 2014 at 16:18
  • Can you show us the code where you call the function you defined ? Commented Mar 13, 2014 at 16:19
  • 2
    possible duplicate of Really PHP? "Argument 1 passed to my_function() must be an instance of string, string given" Commented Mar 13, 2014 at 16:19
  • Thanks, i will just need remove string in myfunction(string $....) Commented Mar 13, 2014 at 17:40

2 Answers 2

4

Try removing the primitive data type from the function declaration. PHP does not require type hints for primitive data types.

function myFunction($name) {
    $db = mysql_connect("localhost", "root", "");
    mysql_select_db(...); 

    $insertplayer="INSERT INTO `...`(....)
    VALUES (......')";
    if (!mysql_query($insertplayer,$db))
      {
      die('Error: ' . mysql_error());
      }
    $id = mysql_insert_id();
    echo 'done for player N°'.$id;
    mysql_close($db);
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you don't want to do a block of code to check type at the beginning of each function you are writing looking like this:

function myFunction($myScalar) {
    if (!is_string($myScalar)) {
        throw new Exception(...);
    }

    ...
}

You can register an errorhandler comparing the given type with your hint and if for example string==string you can ignore the error.

See User Notes on the manual page for some examples

http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.