php面向对象编程之构造方法的使用

时间:2026-02-15 03:42:09

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、至此写完了。我们可以运行看到效果:

© 2026 途途旅游
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com