- 简单地说,类型提示意味着提供函数提示以仅接受给定的数据类型。
- 用专业术语来说,类型提示是一种可以强制函数接受所需数据类型的方法。
- 在PHP中,无涯教程可以将类型提示用于对象,数组和可调用数据类型。
例子1
<?php
class a
{
public $c= "hello learnfk";
}
//使用类名参数创建函数
function display(a $a1)
{
//调用变量
echo $a1->c;
}
display(new a());
?>
例子2
<?php
class Test1
{
public $var= "hello learnfk and SSSIT";
}
//使用类名参数创建函数
function typehint(Test1 $t1)
{
//调用变量
echo $t1->var;
}
//以调用名称作为参数的调用函数
typehint(new Test1());
?>