<?
class User{
private $age ;
private $sal ;
private $payoff ; //声明全局属性.
//构造函数,中创建Payoff的对象.
public function __construct(){
$this->payoff = new Payoff();
}
public function getAge(){
return $this->age;
}
public function setAge($age){
$this->age = $age;
}
// 获得工资.
public function getSal(){
$this->sal = $this->payoff->figure($this);
return $this->sal;
}
}
//这是对应工资与年龄关系的类.
class Payoff{
public function figure($a){
$sal =0;
$age = $a->getAge();
if($age >80 || $age <16 ){
$sal = 0;
}elseif ($age > 50){
$sal = 1000;
}else{
$sal = 800;
}
return $sal;
}
}
figure($this)里的$this是User还是Payoff的对象?