When setting attributes in a class, other attributes are dynamically calculated and assigned values.

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

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.