Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report