PHP 5 锟斤拷锟斤拷使锟斤拷锟斤拷锟斤拷约锟斤拷锟斤拷锟斤拷锟斤拷锟侥诧拷锟斤拷锟斤拷锟斤拷指锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟节猴拷锟斤拷原锟斤拷锟斤拷锟斤拷指锟斤拷锟斤拷锟斤拷锟斤拷郑锟斤拷锟斤拷涌冢锟斤拷锟斤拷椋≒HP 5.1 锟金)伙拷锟斤拷 callable锟斤拷PHP 5.4 锟金)★拷锟斤拷锟斤拷锟斤拷锟绞癸拷锟� NULL 锟斤拷为锟斤拷锟斤拷锟斤拷默锟斤拷值锟斤拷锟斤拷么锟节碉拷锟矫猴拷锟斤拷锟斤拷时锟斤拷锟斤拷然锟斤拷锟斤拷使锟斤拷 NULL 锟斤拷为实锟轿★拷
锟斤拷锟揭伙拷锟斤拷锟斤拷涌锟街革拷锟斤拷锟斤拷锟斤拷锟皆硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷械锟斤拷锟斤拷锟斤拷实锟斤拷也锟斤拷锟斤拷恕锟�
锟斤拷锟斤拷约锟斤拷锟斤拷锟斤拷锟斤拷锟节憋拷锟斤拷锟斤拷锟斤拷锟斤拷 int 锟斤拷 string锟斤拷Traits 也锟斤拷锟斤拷锟斤拷
Example #1 锟斤拷锟斤拷约锟斤拷示锟斤拷
<?php
//锟斤拷锟斤拷锟斤拷锟斤拷锟�
class MyClass
{
/**
* 锟斤拷锟皆猴拷锟斤拷
* 锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为 OtherClass 锟斤拷锟揭伙拷锟斤拷锟斤拷锟�
*/
public function test(OtherClass $otherclass) {
echo $otherclass->var;
}
/**
* 锟斤拷一锟斤拷锟斤拷锟皆猴拷锟斤拷
* 锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷
*/
public function test_array(array $input_array) {
print_r($input_array);
}
}
/**
* 锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为锟捷癸拷锟斤拷锟斤拷
*/
public function test_interface(Traversable $iterator) {
echo get_class($iterator);
}
/**
* 锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为锟截碉拷锟斤拷锟斤拷
*/
public function test_callable(callable $callback, $data) {
call_user_func($callback, $data);
}
}
// OtherClass 锟洁定锟斤拷
class OtherClass {
public $var = 'Hello World';
}
?>
锟斤拷锟斤拷锟斤拷锟矫的诧拷锟斤拷锟诫定锟斤拷牟锟斤拷锟斤拷锟斤拷筒锟揭伙拷锟绞憋拷锟斤拷锟斤拷壮锟揭伙拷锟斤拷刹锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
<?php
// 锟斤拷锟斤拷锟斤拷亩锟斤拷锟�
$myclass = new MyClass;
$otherclass = new OtherClass;
// 锟斤拷锟斤拷锟斤拷锟襟:碉拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 OtherClass 锟斤拷锟揭伙拷锟斤拷锟斤拷锟�
$myclass->test('hello');
// 锟斤拷锟斤拷锟斤拷锟襟:碉拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为 OtherClass 锟斤拷锟揭伙拷锟绞碉拷锟�
$foo = new stdClass;
$myclass->test($foo);
// 锟斤拷锟斤拷锟斤拷锟襟:碉拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为 null
$myclass->test(null);
// 锟斤拷确锟斤拷锟斤拷锟� Hello World
$myclass->test($otherclass);
// 锟斤拷锟斤拷锟斤拷锟襟:碉拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷
$myclass->test_array('a string');
// 锟斤拷确锟斤拷锟斤拷锟斤拷锟斤拷锟�
$myclass->test_array(array('a', 'b', 'c'));
// 锟斤拷确锟斤拷锟斤拷锟� ArrayObject
$myclass->test_interface(new ArrayObject(array()));
// 锟斤拷确锟斤拷锟斤拷锟� int(1)
$myclass->test_callable('var_dump', 1);
?>
锟斤拷锟斤拷约锟斤拷锟斤拷只锟斤拷锟斤拷锟斤拷锟斤拷某锟皆憋拷锟斤拷锟斤拷铮诧拷锟绞癸拷锟斤拷诤锟斤拷锟斤拷铮�
<?php
// 锟斤拷锟斤拷锟斤拷锟斤拷锟�
class MyClass {
public $var = 'Hello World';
}
/**
* 锟斤拷锟皆猴拷锟斤拷
* 锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 MyClass 锟斤拷锟揭伙拷锟斤拷锟斤拷锟�
*/
function MyFunction (MyClass $foo) {
echo $foo->var;
}
// 锟斤拷确
$myclass = new MyClass;
MyFunction($myclass);
?>
锟斤拷锟斤拷约锟斤拷锟斤拷锟斤拷 NULL 值锟斤拷
<?php
/* 锟斤拷锟斤拷 NULL 值 */
function test(stdClass $obj = NULL) {
}
test(NULL);
test(new stdClass);
?>