In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the difference between objects in PHP". In daily operation, I believe that many people have doubts about the difference between objects in PHP. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "differences between objects in PHP". Next, please follow the editor to study!
Object comparison in PHP
In the previous article, we talked about what happens when comparing arrays in PHP. This time, let's talk about how PHP compares when comparing objects.
First, let's define how objects are compared according to the PHP document:
For instances of the same class, compare the attribute sizes. According to the order, different attribute values are encountered and returned after comparison. Subsequent instances of different classes will not be compared. The attribute values must be the same instance.
Let's take a look at an example:
Function bool2str ($bool)
{
If ($bool = false) {
Return 'FALSE'
} else {
Return 'TRUE'
}
}
Function compareObjects (& $o1, & $O2)
{
Echo 'o1 = = O2:'. Bool2str ($o1 = = $O2). "\ n"
Echo 'o1 = = O2:'. Bool2str ($o1 = = $O2). "\ n"
}
Class A {
Private $t = true
Public function setT ($t) {
$this- > t = $t
}
}
Class B {
Protected $t = true
Public function setT1 ($t) {
$this- > t = $t
}
}
Class C {
Private $t = true
Public function setT ($t) {
$this- > t = $t
}
}
$A1 = new A ()
$a2 = new A ()
CompareObjects ($A1, $a2); / / same class
/ / o1 = = O2: TRUE
/ / o1 = = O2: FALSE
$A11 = $A1
CompareObjects ($A1, $A11); / / same instance
/ / o1 = = O2: TRUE
/ / o1 = = O2: TRUE
$a11-> setT (false)
CompareObjects ($A1, $A11); / / different values for the same instance
/ / o1 = = O2: TRUE
/ / o1 = = O2: TRUE
B = new B ()
CompareObjects ($A1, $b); / / different classes
/ / o1 = = O2: FALSE
/ / o1 = = O2: FALSE
C = new C ()
CompareObjects ($A1, $b); / / different classes with the same attribute
/ / o1 = = O2: FALSE
/ / o1 = = O2: FALSE
From the example, we can see that basically all of the above three conditions are met, but it is important to note that in the case of = =, if it is the same instance object, different attribute values will return TRUE. Let's look at a more complex example:
C = new stdClass ()
$d = new stdClass ()
$c-> T1 ='c'
$c-> T2 = 10
$c-> T3 = 50
$d-> T1 ='c'
$d-> T2 = 11
$d-> T3 = 40
Echo'c > dvana, $c > $d? 'TRUE': 'FALSE', PHP_EOL; / / FALSE
Echo'c
< d:', $c < $d ? 'TRUE' : 'FALSE', PHP_EOL; // TRUE $d->T2 = 10; / / $T2 attribute changed to equal
Echo'c > dvana, $c > $d? 'TRUE': 'FALSE', PHP_EOL; / / TRUE
Echo'c
< d:', $c < $d ? 'TRUE' : 'FALSE', PHP_EOL; // FALSE $d->T3 = 50; / / $c, $d attributes are all equal
Echo'c > = dvana, $c > = $d? 'TRUE': 'FALSE', PHP_EOL; / / TRUE
Echo'c C1, $c > $C1? 'TRUE': 'FALSE', PHP_EOL; / / TRUE
Echo'c
< c1:', $c < $c1 ? 'TRUE' : 'FALSE', PHP_EOL; // FALSE echo 'c == c1:', $c == $c1 ? 'TRUE' : 'FALSE', PHP_EOL; // FALSE echo 'c === c1:', $c === $c1 ? 'TRUE' : 'FALSE', PHP_EOL; // FALSE unset($c1->T4)
$C1-> T1 = 'dumped; / / modified a value
Echo'c = = C1, $c = $C1? 'TRUE': 'FALSE', PHP_EOL; / / FALSE
Echo'c = C1 _ 'TRUE': 'FALSE', PHP_EOL; / / FALSE
In this example, we have made a comparison, in which the comparison is based on the attribute value, and the order of the comparison is also the order of the attribute value in English. When\ $T2 has unequal comparison results, $T3 will no longer be compared. In addition, the object after clone is not the original instance object, so the object after clone and the original object cannot use = = to get the same result. When an object has more properties than another, the object is also larger than the object with fewer attributes.
The comparison of objects is actually somewhat similar to arrays, but they are a little different. An important aspect is that grasping them will make attribute comparisons, and there is also the difference of = =. In the array, the type of all attributes must be the same, while the object must be the same instance. And as long as the object is the same instance, using = = will not care about the difference in the value of its properties.
At this point, the study of "the difference between objects 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.