<?php
class person{
var $name;
var $color;
var $sex;
var $age;
function __construct($name,$age='',$sex='boy'){
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
$this->color = 'yello';
}
function eat(){
echo $this->name.'要吃饭';
}
function xinxi(){
echo $this->name.' is '.$this->sex.' and age is '.$this->age.' fuse is '.$this->color;
}
function zuoyong(){
//类似于这样的内部调用,相当于把eat()的代码引入到zuoyong()里面,而不是跳转到eat()里面继续执行
//如果是http://localhost/zuoyong?food=xigua这样的url来调用zuoyong()
//那么eat()中可直接通过$_GET['food']获取url参数,因为全局变量可在函数内部使用
$this->eat();
}
}
?>
son.php
<?php
include('person.class.php');
$son = new person('cuihua',25,'girl');//此处的参数传递要和类的构造方法里面的参数顺序对应
//$son->xinxi();//cuihua is girl and age is 25 fuse is yello
$son->name = '田妞';
$son->eat();//田妞要吃饭
?>