Get the App
SLTechnology News&Howtos  ›  Network Security  › 

_ _ set (), _ _ get (), _ _ isset () and _ _ unset ()

Shulou Source: shulou.com Published: 2022-06-01 01:41:15 09月20日 Update

The _ _ set () and _ _ get () methods are used to obtain and assign values to all private attributes, while the _ _ isset () method is used to check for the existence of private properties, and the _ _ unset () method is used to delete private properties in the object.

1. _ _ set () method

The running result of the program is:

My name: Li Si; gender: female; Age: 20; / / output is the new value in which the private member attribute is reset.

2. _ _ get () method

If the _ _ get () method is declared in the class, this method is automatically called when the value of the private property is obtained directly outside the object, returning the value of the private property. And you can set some conditions in the _ _ get () method to restrict illegal value operations on private properties based on different properties. Like _ _ set (), you need to add it to the class yourself when you declare it before you can use it.

Class person {

Private $name

Private $sex

Private $age

Function _ _ construct ($name= "", $sex= "", $age= "") {

$this- > name=$name

$this- > sex=$sex

$this- > age=$age

}

/ * *

Add the _ _ get () method to the class, use your own bar once when getting the attribute value directly, pass in the attribute name as a parameter and process it.

@ param string $propertyname member attribute name

@ return mixed returns the attribute value

, /

Public function _ _ get ($propertyname) {

If ($propertyname== "sex") {

Return "secrecy"

} elseif ($propertyname== "age") {

If ($this- > age > 30) {

Return $this- > age-10

} else {

Return $this- > $propertyname

}

} else {

Return $this- > $propertyname

}

}

}

$person1=new person ("Zhang San", "male", "20")

Echo "name:". $person1- > name. ""; / / directly access the private property name, and automatically call the _ _ get () method to obtain the

Echo "gender:". $person1- > sex. "; / / the _ _ get () method is called automatically, but no real attribute value is returned in the method.

Echo "age:. $person1- > age."; / / the _ _ get () method is called automatically, and different values are returned depending on the object itself.

3. _ _ isset () method

4. _ _ unset () method

Tags: Attribute method private parameter member different object age gender two name condition Zhang San Li Si run human function masterpiece place name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn Xiaomi NVidia OPPO Reno