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...