看手册 Type Hinting PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1). Class a{ public function x(b $b){ $b->say(); } } Class b{ public function say() { echo "hello world!"; } } class c{ public function say() { echo "hello php!"; } } $b = new b(); $c = new c(); a::x($b);//works! a::x($c);//Fatal error: Argument 1 passed to a::x() must be an instance of b
如果是的话,传不了
Type Hinting
PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1). Class a{
public function x(b $b){
$b->say();
}
}
Class b{
public function say()
{
echo "hello world!";
}
} class c{
public function say()
{
echo "hello php!";
}
}
$b = new b();
$c = new c();
a::x($b);//works!
a::x($c);//Fatal error: Argument 1 passed to a::x() must be an instance of b