Assignment of class variables by php

Source: Internet
Author: User
Assignment of class variables by php
Private $ pagebarnum = 10; $ this-> pagebarnum = $ array ['page _ name']; $ this-> set ('pagebarnum ', $ array ['page _ name']);/*** sets the value of the variable name specified in the class. if the change quantity does not belong to this class, throw an exception * @ param unknown $ var * @ param unknown $ value */function set ($ var, $ value) {if (in_array ($ var, get_object_vars ($ this) {$ this-> $ var = $ value;} else {$ this-> error (_ FUNCTION __, $ var. "does not belong to PB_Page! ");}}

$ This-> pagebarnum = $ array ['page _ name']; and $ this-> set ('pagebarnum', $ array ['page _ name']); what is the difference between values? Obviously, I have defined $ pagebarnum in the class. is it necessary for me to assign values using set?


Reply to discussion (solution)

Variable initialization and adjustment are of course necessary. Suppose you need 10 parameters for a page and 5 parameters for a page. are you doing two classes?

Variable initialization and adjustment are of course necessary. Suppose you need 10 parameters for a page and 5 parameters for a page. are you doing two classes?


If you instantiate a class and pass the parameter in, you can directly use $ this-> pagebarnum = $ array ['page _ name']. what is the difference between checking the existence of variables in the set method? Pagebarnum has defined the meaning of checking when I create a class.

$ This-> pagebarnum = $ array ['page _ name'];
$ This-> set ('pagebarnum', $ array ['page _ name']);
The difference is that the latter does not assign values to non-existent pagebarnum attributes, while the former creates a public pagebarnum attribute for non-existent attributes pagebarnum.

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.