PHP special methods _ set (), _ get () _ PHP Tutorial

Source: Internet
Author: User
Special PHP methods: _ set () and _ get (). Special PHP methods _ set () and _ get ()? Phpheader (Content-type: texthtml; charsetutf-8); classperson {member attribute private $ name; private $ age20; member method functionusern PHP special method _ set (), _ get ()
 
 Name = $ name;} // _ set () is used to set the private property function _ set ($ property_name, $ value) {echo "when setting the private property value directly, the _ set () method is automatically called to assign values to private attributes.
"; $ This-> $ property_name = $ value;} // _ get () method used to obtain the private property function _ get ($ property_name) {echo "the _ get () method is automatically called when the private property value is directly obtained.
";}}$ Obj = new person (); echo $ obj-> username (" James "); echo" "; $ Obj-> name =" "; // when the private property value is directly set, the _ set () method is automatically called to assign an echo value to the private property" "; Echo $ obj-> age; // The _ get () method is automatically called when the private property value is directly obtained, if you do not set the get special method, the following error will be reported // echo $ obj-> age; // when the member attribute or method has private modification before it, it cannot be accessed outside the class. Otherwise, the following error is reported:/* Fatal error: Cannot access private property person: $ age in D: \ wamp \ www \ ajax \ index. php on line 23 */?>

Http://www.bkjia.com/PHPjc/1073354.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1073354.htmlTechArticlePHP special method _ set (), _ get ()? Php header (Content-type: text/html; charset = utf-8); class person {// member attribute private $ name; private $ age = 20; // member method function usern...

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.