关于类里设置属性的同时,动态给其他属性计算并赋值
先看代码:
<br />class test(){<br />public $mPageNo = 1;<br />public $mPageSize = 20;<br />private $mPageOffset = 0;<br />}<br />
登入後複製
请教,如何实现当给$mPageNo或者$mPageSize赋值的时候,就能自动给$mPageOffset赋值为($mPageNo-1)*$mPageSize ?
php class ------解决方案--------------------class test {
private $mPage_no = 1; //页码
private $mPage_size = 40; //每页条数
private $mPageOffset = 0;
function __set($property, $value) {
$this->{$property} = $value; // __set 并不会自动赋值
if ($property=='mPage_no'
------解决方案-------------------- $property=='mPage_Size') {
$this->mPageOffset = (($this->mPage_no)-1) * ($this->mPage_size); }
}
function __get($property) {
return $this->$property;
}
}
$t = new test();
// $t->page_no = 2; 变量名错误,且需要注意区分大小写
$t->mPage_no = 2;
print_r($t->mPageOffset);