龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php5类的类型提示

时间:2014-07-22 14:50来源: 作者: 点击:
分享到:
span style=font-family:Verdana, sans-serif, 宋体;font-size:14px;line-height:22px;background-color:#F9F9F9;php面相对象中参数变量可以包含任何基本类型的数据,参数默认情况下也可以包含任何类型的对象,这中灵
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方法中
精彩图集

赞助商链接