php面向对象编程之构造方法的使用
1、先定义类。<?php class Person{}?>.在类里有三个属性,public $age;public $sex; public $name;
2、接着写构造方法。function __construct($age,$sex,$name){$this->age=$age;
$this->name=$name;
$this->sex=$sex;
echo "I am a".$this->sex.",I am ".$this->age.", my name is ".$this->name;
}
3、然后我们new一个类。继续写:$p1= new Person(15,'girl','JILLY');
4、至此写完了。我们可以运行看到效果:
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:39
阅读量:100
阅读量:80
阅读量:51
阅读量:156