php构造方法
php构造方法
php构造方法,也叫构造函数,当实例化对象的时候自动执行。
语法:
function __construct(){ //两个小划线
}
代码示例:
class Student{ private $name; private $sex; public function __construct(){ echo '这是构造方法'; } //和类名同名的方法是构造方法,在PHP中不建议使用 public function Student() { } } stu1 = new Student;
class Student{ private $name; private $sex; public function __construct($name,$sex){ $this->name = $name; $this->sex =$sex; } public function show() { echo '姓名:'.$this->name; echo '性别:'.$this->sex; } } $stu1 = new Student('清宇','男'); $stu1->show();
上一篇:php算数运算符总结
下一篇:MySQL 复制表