2013-04-22 27 views
-1
<?php 
function baz(ReflectionFunction $a, $b = 1, $c = null) { } 
$reflect = new ReflectionFunction("baz"); 
echo $reflect; 
... 
?> 

問: 這是什麼意思:ReflectionFunction $a?這相當於:$a = new ReflectionFunction()一個問題關於PHP函數參數

回答

1
function baz(ReflectionFunction $a, ... 

是所謂的type hint。這允許PHP解釋器在運行時檢查參數類型

如果您傳遞的值不是ReflectionFunction,那麼PHP會引發致命錯誤。這是非常有幫助的,以從中因爲PHP

的鬆散打字系統引起的,監督許多編程錯誤stabelize你的代碼,並幫助

你可以看到它在影響您是否通過對實例的字符串:

baz('my_function'); 

什麼會給你:

Catchable fatal error: Argument 1 passed to baz() must be an instance of ReflectionFunction, string given, called in /tmp/peerindex-api-consumer/a.php on line 5 and defined in /tmp/peerindex-api-consumer/a.php on line 3