PHP Special Method Set (), Get (), Isset (), and unset ()

Source: Internet
Author: User
Tags access properties
  • The __set () method is used to set the private property value.
  • The __get () method is used to get the private property value.
  • The __isset () method is used to detect whether a private property value is set.
  • The __unset () method is used to delete a private property.
  • In practice, the properties of a class are often set to private, which makes it cumbersome to access properties. Although it is possible to write access to a property as a method, PHP provides some special methods to facilitate such operations.

    __set ()

    The __set () method is used to set the value of a private property:

    function __set ($property _name, $value) {     $this, $property _name = $value;}

    After using the __set () method inside the class, when using the $p 1->name = "Zhang San"; This way, when you set the value of an object's private property, the __set () method is called automatically to set the value of the private property.

    __get ()

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

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

    Example:

     
      ";        $this $property _name = $value;     }    The __get () method is used to get the private attribute    function __get ($property _name) {          echo] automatically calls the __get () method when it obtains the value of the private property directly
    "; Return Isset ($this-$property _name)? $this $property _name:null; }} $p 1=new person ();//The operation that assigns a value directly to a private property automatically calls the __set () method for assignment $p1->name = "Zhang San";//Gets the value of the private property, automatically calls the __get () method, returns the value of the member property echo " My name is: ". $p 1->name;? >

    To run the example, output:

    The __set () method is automatically called when the private property value is set directly to the private property value when the private property is obtained directly, the __get () method is called automatically. My name is: Zhang San

    __isset ()

    The __isset () method is used to detect whether a private property value is set.

    If the member inside the object is public, you can use the Isset () function directly. If it is a private member property, then you need to add a __isset () method to the class:

    Private Function __isset ($property _name) {    return isset ($this, $property _name);}

    This automatically calls the __isset () method when the Isset () function is used outside the class to determine whether the private member inside the object is set.

    __unset ()

    The __unset () method is used to delete a private property.

    As with the Isset () function, the unset () function can only delete the public member properties of an object, and the __unset () method is required when you want to delete private member properties inside the object:

    Private Function __unset ($property _name) {    unset ($this, $property _name);}

  • The __set () method is used to set the private property value.
  • The __get () method is used to get the private property value.
  • The __isset () method is used to detect whether a private property value is set.
  • The __unset () method is used to delete a private property.
  • In practice, the properties of a class are often set to private, which makes it cumbersome to access properties. Although it is possible to write access to a property as a method, PHP provides some special methods to facilitate such operations.

    __set ()

    The __set () method is used to set the value of a private property:

    function __set ($property _name, $value) {     $this, $property _name = $value;}

    After using the __set () method inside the class, when using the $p 1->name = "Zhang San"; This way, when you set the value of an object's private property, the __set () method is called automatically to set the value of the private property.

    __get ()

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

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

    Example:

     
      ";        $this $property _name = $value;     }    The __get () method is used to get the private attribute    function __get ($property _name) {          echo] automatically calls the __get () method when it obtains the value of the private property directly
    "; Return Isset ($this-$property _name)? $this $property _name:null; }} $p 1=new person ();//The operation that assigns a value directly to a private property automatically calls the __set () method for assignment $p1->name = "Zhang San";//Gets the value of the private property, automatically calls the __get () method, returns the value of the member property echo " My name is: ". $p 1->name;? >

    To run the example, output:

    The __set () method is automatically called when the private property value is set directly to the private property value when the private property is obtained directly, the __get () method is called automatically. My name is: Zhang San

    __isset ()

    The __isset () method is used to detect whether a private property value is set.

    If the member inside the object is public, you can use the Isset () function directly. If it is a private member property, then you need to add a __isset () method to the class:

    Private Function __isset ($property _name) {    return isset ($this, $property _name);}

    This automatically calls the __isset () method when the Isset () function is used outside the class to determine whether the private member inside the object is set.

    __unset ()

    The __unset () method is used to delete a private property.

    As with the Isset () function, the unset () function can only delete the public member properties of an object, and the __unset () method is required when you want to delete private member properties inside the object:

    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.