Home > Backend Development > PHP Tutorial > 关于种里设置属性的同时,动态给其他属性计算并赋值

关于种里设置属性的同时,动态给其他属性计算并赋值

WBOY
Release: 2016-06-13 11:19:08
Original
834 people have browsed it

关于类里设置属性的同时,动态给其他属性计算并赋值
先看代码:

<br />class test(){<br />public $mPageNo = 1;<br />public $mPageSize = 20;<br />private $mPageOffset = 0;<br />}<br />
Copy after login


请教,如何实现当给$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);
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template