Home >php教程 >PHP源码 >php5类的类型提示

php5类的类型提示

PHP中文网
PHP中文网Original
2016-05-25 17:12:121217browse

php5类的类型提示

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."&nbsp";
		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)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn