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中文网(m.sbmmt.com)!