


We often see the usage of things like __set __get __isset __unset in PHP object-oriented, but we don’t understand why these things are used. Let’s introduce their usage one by one.
Generally speaking, always define the attributes of a class as private, which is more in line with realistic logic. However, reading and assigning operations to attributes are very frequent, so in PHP5, two functions "__get()" and "__set()" are predefined to obtain and assign their attributes, as well as "__isset" to check the attributes. ()" and the method to delete attributes "__unset()".
In the previous section, we set and obtained methods for each attribute. PHP5 provides us with special methods for setting and obtaining values for attributes, "__set()" and "__get()" These two methods, these two methods do not exist by default, but are manually added to the class. Like the constructor method (__construct()), it will only exist if it is added to the class. You can add it in the following way. Of course, these two methods can also be added according to personal style:
//__get() method is used to obtain private attributes
The code is as follows | Copy code|||
代码如下 | 复制代码 |
private function __get($property_name)
|
__get() method: This method is used to get the private member attribute value. It has one parameter. The parameter is passed in the name of the member attribute you want to get, and the obtained attribute value is returned. This method is not used We call it manually, because we can also make this method a private method, which is automatically called by the object when the private property is directly obtained. Because the private properties have been encapsulated, the value cannot be obtained directly (for example: "echo $p1->name" is wrong to obtain directly), but if you add this method to the class, use " When a statement like "echo $p1->name" directly obtains the value, the __get($property_name) method will be automatically called, and the property name will be passed to the parameter $property_name. Through the internal execution of this method, the private value we passed in will be returned. The value of the attribute. If the member properties are not encapsulated as private, the object itself will not automatically call this method.
__set() method: This method is used to set values for private member attributes. It has two parameters. The first parameter is the name of the attribute you want to set the value for, and the second parameter is the value you want to set for the attribute. , no return value. This method also does not need to be called manually. It can also be made private. It is automatically called when directly setting the private attribute value. The same private attribute has been encapsulated with
. If there is no __set () This method is not allowed, for example: $this->name='zhangsan', this will cause an error, but if you add the __set($property_name, $value) method to the class, you can directly When assigning a value to a private attribute, it will be called automatically, passing the attribute such as name to $property_name, and passing the value "zhangsan" to be assigned to $value. Through the execution of this method, the purpose of assignment is achieved. If the member properties are 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:
The code is as follows | Copy code | ||||
";if(isset($this->$property_name)){return($this->$property_name); }else{return(NULL); }}//__set() method is used to set private properties private function __set($property_name, $value){echo "When directly setting the value of a private property, this __set() method is automatically called to assign a value to the private property "; $this->$property_name = $value;}}$p1=new Person();//When directly assigning a value to a private attribute, the __set() method will be automatically called for assignment $p1->name="张三";$p1->sex="Male";$p1->age =20;//Get the value of the private attribute directly, the __get() method will be automatically called to return the value of the member attributeecho "Name:". $p1->name." ";echo "Gender:".$p1->sex." ";echo "Age:".$p1->age." ";?> |
Program execution results:
When directly setting the value of a private attribute, the __set() method is automatically called to assign a value to the private attribute
When directly setting the value of a private attribute, this __set is automatically called. The () method assigns values to private attributes
When directly setting the value of a private attribute, the __set() method automatically calls the __set() method to assign a value to a private attribute
When directly obtaining the value of a private attribute, the __get is automatically called () method
Name: Zhang San
When directly obtaining the private attribute value, the __get() method is automatically called
Gender: Male
When directly obtaining the private attribute value, the __get() method is automatically called This __get() method is called
Age: 20
If the above code does not add the __get() and __set() methods, the program will error because private operations cannot be performed outside the class members, and the above code helps us directly access the encapsulated private members by automatically calling the __get() and __set() methods.
__isset() method: Before looking at this method, let’s take a look at the application of the “isset()” function. isset() is a function used to determine whether a variable is set. Pass in a variable as a parameter. If the passed-in variable Returns true if it exists, otherwise returns false. So if you use the "isset()" function outside an object to determine whether the members inside the object are set, can you use it? There are two situations. If the members in the object are public, we can use this function to measure the member attributes. If they are private member attributes, this function will not work. The reason is that the private ones are encapsulated and are not exposed externally. Invisible. So we can't use the "isset()" function outside the object to determine whether the private member attributes have been set? Yes, you just need to add a "__isset()" method to the class. When the "isset()" function is used outside the class to determine whether the private members in the object are set, it will be automatically called inside the class. The "__isset()" method helps us complete such operations, and the "__isset()" method can also be made private. You can just add the following code to the class:
The code is as follows | Copy code | ||||
";return isset($this->$nm);} |
__unset() method: Before looking at this method, let’s take a look at the "unset()" function. The function of "unset()" is to delete the specified variable and return true. The parameters are deleted variable. So if you want to delete the member attributes inside the object outside an object, can you use the "unset()" function? There are two situations. If the member attributes inside an object are public, you can use this function to delete them outside the object. The public attributes of the object. If the member attributes of the object are private, I will not have the permission to delete them using this function. But similarly, if you add the "__unset()" method to an object, you can delete it from outside the object. Private member properties of the object. After adding the "__unset()" method to the object, when using the "unset()" function outside the object to delete the private member attributes inside the object, the "__unset()" function is automatically called to help us delete the object. Internal private member attribute, this method can also be defined as private inside the class. Just add the following code to the object:
The code is as follows | Copy code
|
||||
private function __unset($nm) { | echo "When in class Automatically called when the unset() function is used externally to delete private members
unset($this->$nm); //The following are the member attributes of the person
";return isset($this->$nm);}//__unset() methodprivate function __unset($nm){echo "When in
";unset($this->$nm); is automatically called when the unset() function is used outside the class to delete private members. }}$p1=new Person();$p1->name="this is a person name ";//When using the isset() function to measure private members, the __isset() method is automatically called to help us complete it, and the return result is trueecho var_dump (isset($p1->name))."
";echo $p1->name."
";//When using the unset() function to delete private members, the __unset() method is automatically called to help us complete the task and delete the name private attribute unset($p1->name);//has been deleted, so there will be no output for this lineecho $p1->name;?> ; The output result is: When the isset() function determines a private member, it automatically calls bool(true) this is a person nameWhen the unset() function is used outside the class to delete private members, it is automatically called__set(), __get(), __isset(), and __unset(). These four methods are all added to the object. Automatically called when needed to complete operations on private properties inside the object outside the object http://www.bkjia.com/PHPjc/444688.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444688.htmlTechArticleWe often see the usage of __set __get __isset __unset in the object-oriented php, but it is very I don’t understand why these things are used, let’s introduce them one by one...

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)