php面相对象中参数变量可以包含任何基本类型的数据,参数默认情况下也可以包含任何类型的对象,这中灵活的运用在在方法定义中可能会出现问题,但是php引入类的类型提示
class productwrite
{
public function write(product $product) // 规定必须是product的实例
{
$str = "{$product->title}".
$product->getproducter().
"({$product->price})\n";
print $str;
}
}
class product
{
public $title;
public $b;
public $c;
public $price;
function __construct($title,$b,$c,$price)
{
$this->title = $title;
$this->b = $b;
$this->c = $c;
$this->price = $price;
}
function getproducter()
{
echo $this->b." ";
echo $this->c;
}
}
$product = new product("my antonia","willa","cather","5.99");
$write = new productwrite();
$write->write($product); //把product的实例传入 productwrit的write方法中
以上就是php5类的类型提示的内容,更多相关内容请关注PHP中文网(www.php.cn)!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号