When setting attributes in a class, you can calculate and assign values to other attributes first:
class test(){public $mPageNo = 1;public $mPageSize = 20;private $mPageOffset = 0;}
How can I assign a value to $ mPageNo or $ mPageSize automatically to $ mPageOffset ($ mPageNo-1) * $ mPageSize?
Reply to discussion (solution)
Use the magic method _ set () and change the $ mPageSize $ mPageNo access modifier to private or protected. In addition, for direct reading, another _ get () method is required, which is troublesome.
You can directly write a method setPageOffest ($ pageno = 1, $ pagesize = 20)
Use the magic method _ set () and change the $ mPageSize $ mPageNo access modifier to private or protected. In addition, for direct reading, another _ get () method is required, which is troublesome.
You can directly write a method setPageOffest ($ pageno = 1, $ pagesize = 20)
Class test {private $ mPage_no = 1; // page number private $ mPage_size = 40; // number of entries per page private $ mPageOffset = 0; function _ set ($ property, $ value) {if ($ property = 'mpage _ no' | $ property = 'mpage _ size') {$ this-> mPageOffset = ($ this-> mPage_no) -1) * ($ this-> mPage_size); // print_r ($ this-> mPageOffset);} function _ get ($ property) {return $ this-> $ property ;}}
$t = new test();$t->page_no = 2;print_r($t->mPageOffset);
The result is 0 !! Why?
Class test {
Private $ mPage_no = 1; // page number
Private $ mPage_size = 40; // number of entries per page
Private $ mPageOffset = 0;
Function _ set ($ property, $ value ){
$ This-> {$ property} = $ value; // _ set is not automatically assigned.
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; the variable name is incorrect and case sensitive.
$ T-> mPage_no = 2;
Print_r ($ t-> mPageOffset );