The __set__get__unset__isset of the PHP magic method

Source: Internet
Author: User
Tags vars

[PHP]View Plaincopy print?
  1. <?php
  2. /***
  3. Magic Method:
  4. Refers to methods that are automatically called in some cases, called magic methods
  5. PHP Object-oriented, which provides a few magic methods,
  6. They are characterized by a double underline __
  7. __construct (), __destruct (), __call (), __callstatic (), __get (), __set (), __isset (), __unset (), __sleep (), __wakeup (), __ ToString (), __invoke (), __set_state () and __clone ()
  8. __construct: Construction method
  9. __destruct: The destructor method
  10. __clone (): Clone method that will automatically be called when the object is cloned
  11. ***/
  12. __clone (): Clone method that will automatically be called when the object is cloned
  13. Class Human {
  14. public $age = 22;
  15. Public function __clone () {
  16. Echo ' Someone cloned me! '
  17. }
  18. }
  19. $lisi = new Human ();
  20. $zhangsan = Clone $lisi;
  21. /*
  22. Results:
  23. Someone cloned me! fake
  24. */
  25. ?>
[PHP]View Plaincopy print?
  1. <?php
  2. Magic Method __get
  3. Class Human {
  4. private $money = ' 302 ';
  5. protected $age = 23;
  6. public $name = ' Lily ';
  7. Public function __get ($p) {
  8. echo ' You want to visit my ',$p,' attribute <br/> ';
  9. }
  10. }
  11. $lily = new Human ();
  12. Echo $lily->name; Lily
  13. echo $lily->age; //You want to access my age property
  14. echo $lily->money; //You want to access my Money property
  15. echo $lily->friend; //You want to access my friend Properties
  16. /*
  17. Can be summed up:
  18. When we invoke a property that is not allowed to call on a permission, and a property that does not exist,
  19. __get Magic method will be called automatically,
  20. and automatically, the parameter value is the attribute name.
  21. Process:
  22. $lily->age--is not entitled to-->__get (age);
  23. $lily->friend--does not have this attribute-->__get (' friend ');
  24. In life, you help people guard the shop.
  25. Buy Toothbrush---> Good, give you a toothbrush
  26. Buy Towels---> Good, give you a towel
  27. This POS machine is very good---> (pos is the tool of the store, private, not sold: "You do not have the right to buy"), but we use the __get method,
  28. There is a friendly opportunity to deal with it.
  29. The system will directly error, even fatal error, through __get, we can customize user access, the processing behavior.
  30. */
  31. echo ' <br/> ';
  32. Set properties that are not
  33. $lily->aaa = 111;
  34. $lily->bbb = 222;
  35. Print_r ($lily);
  36. /*
  37. Results
  38. Human Object
  39. (
  40. [Money:Human:private] = 302
  41. [age:protected] = 23
  42. [Name] = Lily
  43. [AAA] = 111//Added properties
  44. [BBB] = 222//Added properties
  45. )
  46. */
  47. /*
  48. Even added it.
  49. In fact, the object is a set of properties, in JS more obvious.
  50. If you can add attributes so casually, this object has too many attributes and is not manageable.
  51. */
  52. ?>
[PHP]View Plaincopy print?
  1. <p><?php
  2. Magic Method __set
  3. Class Stu {
  4. private $money = ' 302 ';
  5. protected $age = 23;
  6. public $name = ' Hmm ';
  7. Public function __set ($a,$b) {
  8. echo ' You want to set my ',$a,' attributes ',' <br/> ';
  9. echo ' and value is ',$b,' <br/> ';
  10. }
  11. }
  12. echo '
  13. $HMM = new Stu ();
  14. $HMM->aaa = 111; //You want to set my AAA property and the value is 111
  15. $HMM->money = ' 402 '; You want to set my Money property and the value is 402
  16. $HMM->age = ' 28 '; //You want to set my age property and the value is
  17. Print_r ($hmm);
  18. /*
  19. Results
  20. Stu Object
  21. (
  22. [Money:Stu:private] = 302
  23. [age:protected] = 23
  24. [Name] + Hmm
  25. )
  26. */
  27. $HMM->name = ' hanmm ';
  28. Print_r ($hmm);
  29. /*
  30. Results
  31. Stu Object
  32. (
  33. [Money:Stu:private] = 302
  34. [age:protected] = 23
  35. [Name] = HANMM
  36. ) </p><p>*/
  37. /*
  38. As above, summed up the role of __set
  39. When you assign a value to a property that is not authorized to operate,
  40. Or a non-existent attribute is assigned a value,
  41. __set () Automatic call
  42. and automatically passes 2 parameter attribute property values
  43. Cases:
  44. $HMM->age =---not entitled to---> __set (' Age ', 28);
  45. */</p><p>?></p>
[PHP]View Plaincopy print?
  1. <?php
  2. Magic Method __isset __unset
  3. echo '
  4. Class Dog {
  5. public $leg = 4;
  6. protected $bone = ' pig leg bone ';
  7. Public function __isset ($p) {
  8. echo ' You want to judge me ',$p,' attribute exists <br/> ';
  9. return 1;
  10. }
  11. Public function __unset ($p) {
  12. echo ' you want to get rid of my ',$p,'?!   <br/> ';
  13. }
  14. }
  15. $hua = new Dog ();
  16. if (Isset ($hua->leg)) {
  17. echo $hua->leg,' <br/> ';
  18. }
  19. /*
  20. Results: 4
  21. */
  22. if (Isset ($hua->tail)) {
  23. echo ' has tail attribute ';
  24. } Else {
  25. echo ' no tail ';
  26. }
  27. /*
  28. Results:
  29. You want to tell me that my tail property doesn't exist.
  30. Has tail property
  31. */
  32. /***
  33. __isset () method,
  34. When using Isset () to determine an object that is not visible (protected/private/a property that does not exist)
  35. Will cause __isset () to execute
  36. Q: Isset ($obj->xyz) property, True,
  37. Can you explain that the class declares an XYZ property?
  38. Answer: No
  39. ***/
  40. echo '
  41. echo ' __unset test <br/> ';
  42. Print_r ($hua);
  43. /*
  44. Results:
  45. __unset Test
  46. Dog Object
  47. (
  48. [Leg] = 4
  49. [bone:protected] = pork leg Bone
  50. )
  51. */
  52. unset ($hua->leg);
  53. Print_r ($hua);
  54. /*
  55. Results:
  56. Dog Object
  57. (
  58. [bone:protected] = pork leg Bone
  59. )
  60. */
  61. unset ($hua->tail);
  62. unset ($hua->bone);
  63. /*
  64. Results:
  65. You want to get rid of my tail?!.
  66. You want to get rid of my bone?!.
  67. */
  68. /***
  69. __unset () method
  70. When you destroy an object's invisible property with unset,
  71. Will trigger __unset ();
  72. unset ($hua->tail)----No tail attribute---->__unset (' tail ');
  73. ***/
  74. ?>


The __set__get__unset__isset of the PHP magic method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.