php实例化类

来源:互联网 发布:linux时钟中断 编辑:程序博客网 时间:2024/06/03 23:39

一 代码

<?phpclass Student{    private $name;     //定义类的属性private $age;public function __construct($name, $age)     //定义构造方法{    $this->name = $name;$this->age = $age;} public function getNameAndAge()     //定义类的成员方法{    return "学生" . $this->name . "今年" . $this->age . "周岁";} }$student = new Student("小明", 15);    //类的实例化echo $student->getNameAndAge();    //调用类的成员方法?>

 

二 运行结果
学生小明今年15周岁
原创粉丝点击