php实例化类

一 代码

<?php
class 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周岁

猜你喜欢

转载自cakin24.iteye.com/blog/2376900