码迷,mamicode.com
首页 > Web开发 > 详细

PHP Magic Method Setter and Getter

时间:2020-01-27 20:43:06      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:optional   function   magic   att   start   ble   cte   without   AMM   

<?php
/*
Magic method __set() and __get()
        1.The definition of a magic function is provided by the programmer – meaning you, as the programmer, will actually write
        the definition. This is important to remember – PHP does not provide the definitions of the magic functions
        – the programmer must actually write the code that defines what the magic function will do. But, magic functions will
        never directly be called by the programmer – actually, PHP will call the function ‘behind the scenes’. This is why they
        are called ‘magic’ functions – because they are never directly called, and they allow the programmer to do some pretty
        powerful things.
        2.PHP functions that start with a double underscore – a “__” – are called magic functions (and/or methods) in PHP.
         They are functions that are always defined inside classes, and are not stand-alone (outside of classes) functions.
        The magic functions available in PHP are: __construct(), __destruct(), __call(), __callStatic(), __get(), __set(),
        __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone(), and __autoload()
        3.magic methods can be defined in such a way that they can be called automatically and does not require any function call
        to execute the code inside these functions.
        4.These special functions should be defined by the user. But no need to call them explicitly. Rather, it will be called
             on appropriate event occurrence. For example, class __construct() will be called while instantiating the class.
            PHP magic methods must be defined inside the class.
        get more info at https://phppot.com/php/php-magic-methods/
*/
       //Example address http://www.learningaboutelectronics.com/Articles/Get-and-set-magic-methods-in-PHP.php
        class kids
        {
            private $age;
            protected  $height =23;
            //setter will be invoked when something outside class access inaccessible variables, for encapsulation
            //including protected, private and not declared variable,
            // but public variable will not invoke the setter and getter, because we can access public variable
            public function __set($property, $value)
            {
                if ($value > 30)
                {
                    $current =$this->$property;
                    //notice: $this->property(without $) will create a new attribute called property, $property
                    $this->$property= $value;
                    echo "You are going to update $property from $current to $value"."<br>";
                }else{
                    echo "Update $property failed!<br>";
                }
            }
            public function __get($property)
            {
                return "You are trying to get inaccessible $property 's value: " . $this->$property . "<br>";
            }

        }
        $kid= new kids;  //() is optional, automatically call __constructor(){}
        //1. testing protected variable
                $kid->height =55; //this will implicitly and automatically call __set() because height property is protected
                                //You are going to update height from 23 to 55
                $kid->height =23; //Update height failed!
                echo $kid->height;   //You are trying to get inaccessible height 's value: 55
        //2. testing private variable
                $kid->age = 37;  //You are going to update age from to 37
                echo $kid->age;    //You are trying to get inaccessible age 's value: 37
        //3.testing undeclared variable in the class
                $kid->weight =40;
                /*You are going to update weight from You are trying to get inaccessible weight 's value:
                to 40  */ //why ?
                //because $current=$this->$property =$this->weight statement invoke the getter
               echo $kid->weight; //40

PHP Magic Method Setter and Getter

标签:optional   function   magic   att   start   ble   cte   without   AMM   

原文地址:https://www.cnblogs.com/luoxuw/p/12236725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!