douhan1860 2013-05-20 06:15
浏览 112
已采纳

怎么PDO可以有类型字符串参数的方法,但我不能在我自己的函数中这样做?

If I create an instance of PDO and then call PDO->Quote('test') it works no problems.

If I look at the defination of the PDO Quote method it looks like this:

/**
 * Quotes a string for use in a query.
 * PDO::quote() places quotes around the input string (if required) and escapes special characters within the input string, using a quoting style appropriate to the underlying driver.
 *
 * @param string $string The string to be quoted.
 * @param int $parameter_type Provides a data type hint for drivers that have alternate quoting styles.
 *
 * return string
 */
function quote(string $string, int $parameter_type) {/* method implementation */}

Note the parameters actully have types defined in the method signature, string and int.

Now if I create a function like this:

function Test(string $test) {
    return $test;
}

And attempt to call it like this:

echo Test('test');

It fails with the following error:

( ! ) Catchable fatal error: Argument 1 passed to Test() must be an instance of string, string given, called in [path_removed]TestTypeHinting.php on line 36 and defined in [path_removed]TestTypeHinting.php on line 2

How come PDO can do it, but I can't?

Regards,

Scott

  • 写回答

2条回答 默认 最新

  • dongre6270 2013-05-20 06:36
    关注

    It's documentation and the real code. Read about type hinting.

    Type hints can not be used with scalar types such as int or string

    But there is some moving to implement scalar type hinting.

    You can add phpdoc for documentation your function.

    /**
     * Test function
     * @param string $test
     * @return string
     */
    function Test($test) {
        return $test;
    }
    

    Also read about How to read a function definition

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改