When setting attributes in the preceding settings, other attributes are dynamically calculated and assigned values.

Source: Internet
Author: User
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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.