[PHP]View Plaincopy print?
- <?php
- /***
- Magic Method:
- Refers to methods that are automatically called in some cases, called magic methods
- PHP Object-oriented, which provides a few magic methods,
- They are characterized by a double underline __
- __construct (), __destruct (), __call (), __callstatic (), __get (), __set (), __isset (), __unset (), __sleep (), __wakeup (), __ ToString (), __invoke (), __set_state () and __clone ()
- __construct: Construction method
- __destruct: The destructor method
- __clone (): Clone method that will automatically be called when the object is cloned
- ***/
- __clone (): Clone method that will automatically be called when the object is cloned
- Class Human {
- public $age = 22;
- Public function __clone () {
- Echo ' Someone cloned me! '
- }
- }
- $lisi = new Human ();
- $zhangsan = Clone $lisi;
- /*
- Results:
- Someone cloned me! fake
- */
- ?>
[PHP]View Plaincopy print?
- <?php
- Magic Method __get
- Class Human {
- private $money = ' 302 ';
- protected $age = 23;
- public $name = ' Lily ';
- Public function __get ($p) {
- echo ' You want to visit my ',$p,' attribute <br/> ';
- }
- }
- $lily = new Human ();
- Echo $lily->name; Lily
- echo $lily->age; //You want to access my age property
- echo $lily->money; //You want to access my Money property
- echo $lily->friend; //You want to access my friend Properties
- /*
- Can be summed up:
- When we invoke a property that is not allowed to call on a permission, and a property that does not exist,
- __get Magic method will be called automatically,
- and automatically, the parameter value is the attribute name.
- Process:
- $lily->age--is not entitled to-->__get (age);
- $lily->friend--does not have this attribute-->__get (' friend ');
- In life, you help people guard the shop.
- Buy Toothbrush---> Good, give you a toothbrush
- Buy Towels---> Good, give you a towel
- 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,
- There is a friendly opportunity to deal with it.
- The system will directly error, even fatal error, through __get, we can customize user access, the processing behavior.
- */
- echo ' <br/> ';
- Set properties that are not
- $lily->aaa = 111;
- $lily->bbb = 222;
- Print_r ($lily);
- /*
- Results
- Human Object
- (
- [Money:Human:private] = 302
- [age:protected] = 23
- [Name] = Lily
- [AAA] = 111//Added properties
- [BBB] = 222//Added properties
- )
- */
- /*
- Even added it.
- In fact, the object is a set of properties, in JS more obvious.
- If you can add attributes so casually, this object has too many attributes and is not manageable.
- */
- ?>
[PHP]View Plaincopy print?
- <p><?php
- Magic Method __set
- Class Stu {
- private $money = ' 302 ';
- protected $age = 23;
- public $name = ' Hmm ';
- Public function __set ($a,$b) {
- echo ' You want to set my ',$a,' attributes ',' <br/> ';
- echo ' and value is ',$b,' <br/> ';
- }
- }
- echo '
- $HMM = new Stu ();
- $HMM->aaa = 111; //You want to set my AAA property and the value is 111
- $HMM->money = ' 402 '; You want to set my Money property and the value is 402
- $HMM->age = ' 28 '; //You want to set my age property and the value is
- Print_r ($hmm);
- /*
- Results
- Stu Object
- (
- [Money:Stu:private] = 302
- [age:protected] = 23
- [Name] + Hmm
- )
- */
- $HMM->name = ' hanmm ';
- Print_r ($hmm);
- /*
- Results
- Stu Object
- (
- [Money:Stu:private] = 302
- [age:protected] = 23
- [Name] = HANMM
- ) </p><p>*/
- /*
- As above, summed up the role of __set
- When you assign a value to a property that is not authorized to operate,
- Or a non-existent attribute is assigned a value,
- __set () Automatic call
- and automatically passes 2 parameter attribute property values
- Cases:
- $HMM->age =---not entitled to---> __set (' Age ', 28);
- */</p><p>?></p>
[PHP]View Plaincopy print?
- <?php
- Magic Method __isset __unset
- echo '
- Class Dog {
- public $leg = 4;
- protected $bone = ' pig leg bone ';
- Public function __isset ($p) {
- echo ' You want to judge me ',$p,' attribute exists <br/> ';
- return 1;
- }
- Public function __unset ($p) {
- echo ' you want to get rid of my ',$p,'?! <br/> ';
- }
- }
- $hua = new Dog ();
- if (Isset ($hua->leg)) {
- echo $hua->leg,' <br/> ';
- }
- /*
- Results: 4
- */
- if (Isset ($hua->tail)) {
- echo ' has tail attribute ';
- } Else {
- echo ' no tail ';
- }
- /*
- Results:
- You want to tell me that my tail property doesn't exist.
- Has tail property
- */
- /***
- __isset () method,
- When using Isset () to determine an object that is not visible (protected/private/a property that does not exist)
- Will cause __isset () to execute
- Q: Isset ($obj->xyz) property, True,
- Can you explain that the class declares an XYZ property?
- Answer: No
- ***/
- echo '
- echo ' __unset test <br/> ';
- Print_r ($hua);
- /*
- Results:
- __unset Test
- Dog Object
- (
- [Leg] = 4
- [bone:protected] = pork leg Bone
- )
- */
- unset ($hua->leg);
- Print_r ($hua);
- /*
- Results:
- Dog Object
- (
- [bone:protected] = pork leg Bone
- )
- */
- unset ($hua->tail);
- unset ($hua->bone);
- /*
- Results:
- You want to get rid of my tail?!.
- You want to get rid of my bone?!.
- */
- /***
- __unset () method
- When you destroy an object's invisible property with unset,
- Will trigger __unset ();
- unset ($hua->tail)----No tail attribute---->__unset (' tail ');
- ***/
- ?>
The __set__get__unset__isset of the PHP magic method