Php _ get () _ set () _ construct () function _ construct ($ name, $ sex, $ age, $ school )?????? {????????? $ This-& gt; name $ name ;????????? $ This-& gt; sex $ sex ;????????? $ This-& g php _ get () _ set () _ construct ()
Function _ construct ($ name, $ sex, $ age, $ school)
??? ??? {
??? ??? ??? $ This-> name = $ name;
??? ??? ??? $ This-> sex = $ sex;
??? ??? ??? $ This-> age = $ age;
??? ??? ??? $ This-> school = $ school;
}
?
?
?
Function _ get ($ property_name)
?? {?
??? // Echo "the _ get () method is automatically called when the private property value is directly obtained.
";
???? If (isset ($ this-> $ property_name )){
???? Return ($ this-> $ property_name );
??? } Else {
???? Return (NULL );
???}
??}
?? // _ Set () is used to set private attributes.
?? Function _ set ($ property_name, $ value)
?? {
?? //? Echo "when the private property value is directly set, the _ set () method is automatically called to assign values to the private property.
";
??? $ This-> $ property_name = $ value;
??} ?
?
?
Function setAge ($ age )? // Provide a public age setting method for external users
??? {
??? ??? If ($ age <0 | $ age> 130) // when assigning a value to an attribute, to avoid setting an invalid value to the attribute
??? ??? ??? Return;
??? ??? $ This-> age = $ age;
}
Function getAge ()??? // Provide a public method for obtaining the age.
{
??? Return ($ this-> age );
}
?