2

PDO のインスタンスを作成してから PDO->Quote('test') を呼び出すと、問題なく動作します。

PDO Quote メソッドの定義を見ると、次のようになります。

/**
 * 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 */}

パラメーターには、実際にはメソッド シグネチャ、string、および int で定義された型があることに注意してください。

今、次のような関数を作成すると:

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

そして、次のように呼び出してみてください:

echo Test('test');

次のエラーで失敗します。

( ! ) 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

なぜ PDO ではできるのに、私にはできないのでしょうか?

よろしく、

スコット

4

2 に答える 2

1

それはドキュメントと実際のコードです。タイプヒントについて読んでください。

型ヒントは、int や string などのスカラー型では使用できません

しかし、スカラー型ヒンティングを実装しようという動きがあります。

関数をドキュメント化するために phpdoc を追加できます。

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

関数定義の読み方についてもお読みください

于 2013-05-20T06:36:37.423 に答える