`
mywebcode
  • 浏览: 999641 次
文章分类
社区版块
存档分类
最新评论

PHP中用$this传递对象

 
阅读更多
微笑相对于C和JAVA语言,PHP面向对象从底层就尽量的是程序员运用简单。今天来看一下PHP中用$this来传递对象的操作。下面写一个根据不同年龄发不同工资的类,这里处理年龄和工资的类为一个独立的业务模型。

===================》废话不多说,代码走你《================

<?php
header('content-type:text/html;charset=utf-8');
class Phper{
	private $age;
	private $sal;
	private $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);//结合下面的我通过$this传递对象
		return $this->sal;
	}
}
//下面是工资与年龄关系的类
class Payoff{
	public function figure($a){//结合上面的我通过$this传递对象
		$sal=0;
		$age=$a->getage();//调用Phper类中的方法
		if($age>80||$age<16){
			$sal=0;
		}elseif($age>50){
			$sal=200000;
		}else{
			$sal=18000;
		}
		return $sal;
	}
}
//实例化Phper
$phper=new Phper();
$phper->setage(23);
echo $phper->getage()."age,his sal is ".$phper->getsal()."¥";
echo "<br />";
$phper->setage(55);
echo $phper->getage()."age,his sal is ".$phper->getsal()."¥";
?>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics