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

What are the differences between get and set in php

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the differences between get and set in php". In daily operation, I believe many people have doubts about the differences between get and set in php. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the differences between get and set in php?" Next, please follow the editor to study!

The difference between get and set in php: the "_ _ get ()" method is used to get the property value of the private member, and the result returned is the obtained property value, while the "_ _ set ()" method is used to set the property value of the private member, which does not return a value.

Operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer

What is the difference between get and set in php

Generally speaking, it is more logical to define the attribute of a class as private. However, the reading and assignment of attributes is very frequent, so in PHP5, two functions "_ _ get ()" and "_ _ set ()" are predefined to get and assign their attributes. Similar to the operation of javabean in java, the method is similar, except that there is no need to set and get each field as it does in javabean. You only need to add two magic tricks. That is, the operation of setting and taking values of private members.

In PHP5, we are provided with methods for setting and obtaining values for attributes, "_ _ set ()" and "_ _ get ()". These two methods do not exist by default, but are manually added to the class. Like the constructor (_ _ construct ()), these two methods can be added to the class only after they are added. You can add these two methods as follows, or you can add them according to your own style:

/ / _ _ set () method is used to set the private property public function _ _ set ($name,$value) {$this- > $name = $value;} / / _ _ get () method to get the private property public function _ _ get ($name) {return $this- > $name;}

_ _ get () method: this method is used to obtain the property value of a private member. There is a parameter that passes in the name of the member property you want to obtain and returns the obtained property value. This method does not need to be called manually, because we can also make this method a private method, which is automatically called by the object when the private property is obtained directly. Because the private property has been encapsulated, you can't get the value directly, but if you add this method to the class, when you use a statement like "echo$p1- > name" to get the value directly, you will automatically call the _ _ get ($name) method, pass the property name to the parameter $name, and return the value of the private property we passed in through the internal execution of this method. If the member property is not encapsulated as private, the object itself will not automatically call this method.

_ _ set () method: this method is used to set a value for a private member property, with two parameters, the first parameter is the name of the property you want to set the value for, and the second parameter is the value to be set for the property, with no return value. This method also does not need to be called manually, it can also be made private, it is automatically called when the private property value is set directly, and the private property has been encapsulated.

If there is no _ _ set () method, it is not allowed, for example: $this- > name='zhangsan', this will make an error, but if you add the _ _ set ($property_name, $value) method to the class, it will be automatically called when assigning values to private attributes directly, passing attributes such as name to $property_name, and passing the value to be assigned "zhangsan" to $value, through the execution of this method to achieve the purpose of assignment. If the member property is not encapsulated as private, the object itself will not automatically call this method. In order not to pass in illegal values, you can also make a judgment in this method. The code is as follows:

Class Person {/ / below are the member attributes of a person, all of which are encapsulated private members private $name; / / person's name sub-private $sex; / / person's gender private $age / / the person's age / / _ _ get () method is used to obtain the private attribute public function _ _ get ($property_name) {echo "this _ _ get () method is automatically called when the private attribute value is obtained directly.

; if (isset ($this- > $property_name)) {return ($this- > $property_name);} else {return (NULL) }} / / _ set () method is used to set the private property public function _ _ set ($property_name, $value) {echo "when setting the private property value directly, this _ _ set () method is automatically called to assign a value to the private property.

"; $this- > $property_name = $value;} $p1 = new Person (); / / for operations that directly assign values to private attributes, the _ _ set () method is automatically called to assign $p1-> name =" Zhang San "; $p1-> sex =" male "; $p1-> age = 20; / / if you directly obtain the value of the private attribute, the _ _ get () method is automatically called to return the value of the member attribute" name: ". $p1-> name. "

Echo "gender:". $p1-> sex.

Echo "Age:". $p1-> age. "

"

Program execution result:

When the private property value is set directly, the _ _ set () method is automatically called to assign a value to the private property.

When the private property value is set directly, the _ _ set () method is automatically called to assign a value to the private property.

When the private property value is set directly, the _ _ set () method is automatically called to assign a value to the private property.

This _ _ get () method is called automatically when the private attribute value is obtained directly

Name: Zhang San

This _ _ get () method is called automatically when the private attribute value is obtained directly

Gender: male

This _ _ get () method is called automatically when the private attribute value is obtained directly

Age: 20

If the above code does not add the _ _ get () and _ _ set () methods, the program will make an error because private members cannot be manipulated outside the class, and the above code helps us directly access encapsulated private members by automatically calling the _ _ get () and _ _ set () methods.

At this point, the study on "what is the difference between get and set in php" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report