1 <?php 2 class Person{ //定义了一个Person类 3 public $name; //定义属性name 4 public $age; //定义属性age 5 function __construct($name,$age){ 6 $this->name = $name; 7 $this->age = $age; 8 } 9 10 function say( ){ //定义方法say( ) 11 echo "my name is ".$this->name."<br>"; 12 } 13 } 14 15 $zhangsan=new Person(); //生成对象zhangsan 16 $zhangsan->name="zhangsan"; //给对象中的name赋值 17 $zhangsan->age=26; //给对象中的age赋值 18 $zhangsan->say(); //调用方法say() 19 echo "age is ".$zhangsan->age; //输出age值 20 21 ?>
注:英文句号(.)可将字符串连接合并成新的字符串。
转自:http://www.cnblogs.com/mydomain/archive/2012/10/19/2730331.html