When setting attributes in a class, calculate and assign values to other attributes dynamically. first read the code: class & nbsp; test () {public & nbsp; $ mPageNo & nbsp ;=& nbsp; 1; public & nbsp; $ mPageSize & nbsp ;=& nbsp; 20; private & nbsp; when setting attributes in a class, other attributes are dynamically calculated and assigned values.
First look at the code:
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?
Php class
------ Solution --------------------
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'
------ Solution --------------------
$ 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 );