PHP special methods: set (), get (), isset (), and unset ()

Source: Internet
Author: User
PHP special methods: set (), get (), isset (), and unset ()
  • The _ set () method is used to set private property values.
  • The _ get () method is used to obtain private property values.
  • The _ isset () method is used to check whether the private property value is set.
  • The _ unset () method is used to delete private attributes.
  • In practice, attributes of a class are often set to private, which makes it troublesome to access attributes. Although you can write a method for accessing attributes, PHP provides some special methods to facilitate such operations.

    _ Set ()

    The _ set () method is used to set private property values:

    function __set($property_name, $value){     $this->$property_name = $value; }

    When the _ set () method is used in the class, when $ p1-> name = "zhang san" is used to set the value of the private property of the object, the _ set () method is automatically called to set the value of the private attribute.

    _ Get ()

    The _ get () method is used to obtain the private property value:

    function __set($property_name, $value){     return isset($this->$property_name) ? $this->$property_name : null;}

    Example:

     "; $ 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.
    "; Return isset ($ this-> $ property_name )? $ This-> $ property_name: null ;}$ p1 = new Person (); // an operation that directly assigns a value to a private property will automatically call _ set () method $ p1-> name = "zhang san"; // directly obtain the value of the private property. The _ get () method is automatically called, return the member attribute value echo "My name is :". $ p1-> name;?>

    Run this example and output:

    When setting the private property value directly, the _ set () method is automatically called to assign values to the private property. when obtaining the private property value directly, the _ get () method is automatically called () method: Zhang San
    _ Isset ()

    The _ isset () method is used to check whether the private property value is set.

    If the members in the object are public, you can directly use the isset () function. If it is a private member attribute, you need to add a _ isset () method to the class:

    private function __isset($property_name){    return isset($this->$property_name);}

    In this way, when the isset () function is used outside the class to determine whether the private members in the object are set, the _ isset () method is automatically called for detection.

    _ Unset ()

    The _ unset () method is used to delete private attributes.

    Like the isset () function, the unset () function can only delete the public member attributes of an object. to delete the private member attributes of an object, use the _ unset () method:

    private function __unset($property_name){    unset($this->$property_name);}

  • The _ set () method is used to set private property values.
  • The _ get () method is used to obtain private property values.
  • The _ isset () method is used to check whether the private property value is set.
  • The _ unset () method is used to delete private attributes.
  • In practice, attributes of a class are often set to private, which makes it troublesome to access attributes. Although you can write a method for accessing attributes, PHP provides some special methods to facilitate such operations.

    _ Set ()

    The _ set () method is used to set private property values:

    function __set($property_name, $value){     $this->$property_name = $value; }

    When the _ set () method is used in the class, when $ p1-> name = "zhang san" is used to set the value of the private property of the object, the _ set () method is automatically called to set the value of the private attribute.

    _ Get ()

    The _ get () method is used to obtain the private property value:

    function __set($property_name, $value){     return isset($this->$property_name) ? $this->$property_name : null;}

    Example:

     "; $ 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.
    "; Return isset ($ this-> $ property_name )? $ This-> $ property_name: null ;}$ p1 = new Person (); // an operation that directly assigns a value to a private property will automatically call _ set () method $ p1-> name = "zhang san"; // directly obtain the value of the private property. The _ get () method is automatically called, return the member attribute value echo "My name is :". $ p1-> name;?>

    Run this example and output:

    When setting the private property value directly, the _ set () method is automatically called to assign values to the private property. when obtaining the private property value directly, the _ get () method is automatically called () method: Zhang San
    _ Isset ()

    The _ isset () method is used to check whether the private property value is set.

    If the members in the object are public, you can directly use the isset () function. If it is a private member attribute, you need to add a _ isset () method to the class:

    private function __isset($property_name){    return isset($this->$property_name);}

    In this way, when the isset () function is used outside the class to determine whether the private members in the object are set, the _ isset () method is automatically called for detection.

    _ Unset ()

    The _ unset () method is used to delete private attributes.

    Like the isset () function, the unset () function can only delete the public member attributes of an object. to delete the private member attributes of an object, use the _ unset () method:

    private function __unset($property_name){    unset($this->$property_name);}

    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.