php有一类很神奇的方法,这些方法是保留方法,通常不会在外部被显式调用,他们使用双下划线(__)开头,他们被称为魔术方法(Magic Methods)。php官方也不建议定义其他双下划线开头的方法。
这次介绍属性重载方法:get/set/isset/unset
<span>public</span> void __set ( <span>string</span> <span>$name</span> , <span>mixed</span> <span>$value</span><span> ) </span><span>public</span> <span>mixed</span> __get ( <span>string</span> <span>$name</span><span> ) </span><span>public</span> bool __isset ( <span>string</span> <span>$name</span><span> ) </span><span>public</span> void __unset ( <span>string</span> <span>$name</span> )
这些方法触发的时机,都是在访问不可访问的属性的时候。
如以下:
<span> 1</span> <?<span>php </span><span> 2</span> <span> 3</span> <span>class</span><span> Cls{ </span><span> 4</span> <span> 5</span> <span>private</span> <span>$a</span> = 0<span>; </span><span> 6</span> <span>protected</span> <span>$b</span> = 1<span>; </span><span> 7</span> <span>public</span> <span>$c</span> = 2<span>; </span><span> 8</span> <span>var</span> <span>$d</span> = 3<span>; </span><span> 9</span> <span>10</span> <span>private</span> <span>$_properties</span> = <span>array</span>(0<span>); </span><span>11</span> <span>12</span> <span>public</span> <span>function</span> __set(<span>$name</span>, <span>$value</span><span>){ </span><span>13</span> <span>echo</span> <span>__METHOD__</span> . ' is called! ' . json_encode(<span>func_get_args</span>()) . "\n"<span>; </span><span>14</span> <span>return</span> <span>$this</span>->_properties[<span>$name</span>] = <span>$value</span><span>; </span><span>15</span> <span> } </span><span>16</span> <span>17</span> <span>public</span> <span>function</span> __get(<span>$name</span><span>){ </span><span>18</span> <span>echo</span> <span>__METHOD__</span> . ' is called! ' . json_encode(<span>func_get_args</span>()) . "\n"<span>; </span><span>19</span> <span>return</span> <span>$this</span>->_properties[<span>$name</span><span>]; </span><span>20</span> <span> } </span><span>21</span> <span>22</span> <span>public</span> <span>function</span> __isset(<span>$name</span><span>){ </span><span>23</span> <span>echo</span> <span>__METHOD__</span> . ' is called! ' . json_encode(<span>func_get_args</span>()) . "\n"<span>; </span><span>24</span> <span>return</span> <span>isset</span>(<span>$this</span>->_properties[<span>$name</span><span>]); </span><span>25</span> <span> } </span><span>26</span> <span>27</span> <span>public</span> <span>function</span> __unset(<span>$name</span><span>){ </span><span>28</span> <span>echo</span> <span>__METHOD__</span> . ' is called! ' . json_encode(<span>func_get_args</span>()) . "\n"<span>; </span><span>29</span> <span>unset</span>(<span>$this</span>->_properties[<span>$name</span><span>]); </span><span>30</span> <span> } </span><span>31</span> <span>32</span> <span>public</span> <span>function</span><span> dump(){ </span><span>33</span> <span>echo</span> "====================== DUMP START ====================\n"<span>; </span><span>34</span> <span>echo</span> <<<<span>STR </span><span>35</span> a: <span>$this</span>-><span>a </span><span>36</span> b: <span>$this</span>-><span>b </span><span>37</span> c: <span>$this</span>-><span>c </span><span>38</span> d: <span>$this</span>-><span>d\n </span><span>39</span> <span>STR; </span><span>40</span> <span>foreach</span>(<span>$this</span>->_properties <span>as</span> <span>$k</span> => <span>$v</span><span>){ </span><span>41</span> <span>echo</span> <<<<span>STR </span><span>42</span> Property <span>$k</span>: <span>$v</span><span>\n </span><span>43</span> <span>STR; </span><span>44</span> <span> } </span><span>45</span> <span>46</span> <span>echo</span> "====================== DUMP STOP =====================\n"<span>; </span><span>47</span> <span>48</span> <span> } </span><span>49</span> <span>} </span><span>50</span> <span>$string</span> = 'abcdef'<span>; </span><span>51</span> <span>$obj</span> = <span>new</span><span> Cls(); </span><span>52</span> <span>53</span> <span>$list</span> = <span>array</span>('isset', 'get', 'set', 'isset', 'unset', 'isset'<span>); </span><span>54</span> <span>$obj</span>-><span>dump(); </span><span>55</span> <span>foreach</span>(<span>$list</span> <span>as</span> <span>$method</span><span>){ </span><span>56</span> <span>for</span>(<span>$i</span> = 0 ; <span>$i</span> < <span>strlen</span>(<span>$string</span>) ; <span>$i</span> ++<span>){ </span><span>57</span> <span>$char</span> = <span>$string</span>{<span>$i</span><span>}; </span><span>58</span> <span>echo</span> "------ <span>$method</span> : <span>$char</span> ------\n"<span>; </span><span>59</span> <span>switch</span>(<span>$method</span><span>){ </span><span>60</span> <span>case</span> 'isset': <span>61</span> <span>echo</span> json_encode(<span>$tmp</span> = <span>isset</span>(<span>$obj</span>-><span>$char</span>)) . "\n"<span>; </span><span>62</span> <span>break</span><span>; </span><span>63</span> <span>case</span> 'get': <span>64</span> <span>echo</span> json_encode(<span>$tmp</span> = <span>$obj</span>-><span>$char</span>) . "\n"<span>; </span><span>65</span> <span>break</span><span>; </span><span>66</span> <span>case</span> 'set': <span>67</span> <span>echo</span> json_encode(<span>$tmp</span> = <span>$obj</span>-><span>$char</span> = <span>$char</span> . "-val") . "\n"<span>; </span><span>68</span> <span>break</span><span>; </span><span>69</span> <span>case</span> 'unset': <span>70</span> <span>unset</span>(<span>$obj</span>-><span>$char</span><span>); </span><span>71</span> <span>break</span><span>; </span><span>72</span> <span>default</span>: <span>73</span> <span>break</span><span>; </span><span>74</span> <span> } </span><span>75</span> <span> } </span><span>76</span> <span>$obj</span>-><span>dump(); </span><span>77</span> }
结果输出:
====================== DUMP START ====================<span> a</span>: 0<span> b</span>: 1<span> c</span>: 2<span> d</span>: 3<span> Property </span>0: 0 ====================== DUMP STOP ===================== ------ <span>isset</span> : a ------<span> Cls</span>::__isset is called! ["a"<span>] </span><span>false</span> ------ <span>isset</span> : b ------<span> Cls</span>::__isset is called! ["b"<span>] </span><span>false</span> ------ <span>isset</span> : c ------ <span>true</span> ------ <span>isset</span> : d ------ <span>true</span> ------ <span>isset</span> : e ------<span> Cls</span>::__isset is called! ["e"<span>] </span><span>false</span> ------ <span>isset</span> : f ------<span> Cls</span>::__isset is called! ["f"<span>] </span><span>false</span> ====================== DUMP START ====================<span> a</span>: 0<span> b</span>: 1<span> c</span>: 2<span> d</span>: 3<span> Property </span>0: 0 ====================== DUMP STOP ===================== ------ get : a ------<span> Cls</span>::__get is called! ["a"<span>] </span><span>null</span> ------ get : b ------<span> Cls</span>::__get is called! ["b"<span>] </span><span>null</span> ------ get : c ------ 2 ------ get : d ------ 3 ------ get : e ------<span> Cls</span>::__get is called! ["e"<span>] </span><span>null</span> ------ get : f ------<span> Cls</span>::__get is called! ["f"<span>] </span><span>null</span> ====================== DUMP START ====================<span> a</span>: 0<span> b</span>: 1<span> c</span>: 2<span> d</span>: 3<span> Property </span>0: 0 ====================== DUMP STOP ===================== ------ set : a ------<span> Cls</span>::__set is called! ["a","a-val"<span>] </span>"a-val" ------ set : b ------<span> Cls</span>::__set is called! ["b","b-val"<span>] </span>"b-val" ------ set : c ------ "c-val" ------ set : d ------ "d-val" ------ set : e ------<span> Cls</span>::__set is called! ["e","e-val"<span>] </span>"e-val" ------ set : f ------<span> Cls</span>::__set is called! ["f","f-val"<span>] </span>"f-val" ====================== DUMP START ====================<span> a</span>: 0<span> b</span>: 1<span> c</span>: c-<span>val d</span>: d-<span>val Property </span>0: 0<span> Property a</span>: a-<span>val Property b</span>: b-<span>val Property e</span>: e-<span>val Property f</span>: f-<span>val </span>====================== DUMP STOP ===================== ------ <span>isset</span> : a ------<span> Cls</span>::__isset is called! ["a"<span>] </span><span>true</span> ------ <span>isset</span> : b ------<span> Cls</span>::__isset is called! ["b"<span>] </span><span>true</span> ------ <span>isset</span> : c ------ <span>true</span> ------ <span>isset</span> : d ------ <span>true</span> ------ <span>isset</span> : e ------<span> Cls</span>::__isset is called! ["e"<span>] </span><span>true</span> ------ <span>isset</span> : f ------<span> Cls</span>::__isset is called! ["f"<span>] </span><span>true</span> ====================== DUMP START ====================<span> a</span>: 0<span> b</span>: 1<span> c</span>: c-<span>val d</span>: d-<span>val Property </span>0: 0<span> Property a</span>: a-<span>val Property b</span>: b-<span>val Property e</span>: e-<span>val Property f</span>: f-<span>val </span>====================== DUMP STOP ===================== ------ <span>unset</span> : a ------<span> Cls</span>::__unset is called! ["a"<span>] </span>------ <span>unset</span> : b ------<span> Cls</span>::__unset is called! ["b"<span>] </span>------ <span>unset</span> : c ------ ------ <span>unset</span> : d ------ ------ <span>unset</span> : e ------<span> Cls</span>::__unset is called! ["e"<span>] </span>------ <span>unset</span> : f ------<span> Cls</span>::__unset is called! ["f"<span>] </span>====================== DUMP START ====================<span> Cls</span>::__get is called! ["c"<span>] Cls</span>::__get is called! ["d"<span>] a</span>: 0<span> b</span>: 1<span> c</span>:<span> d</span>:<span> Property </span>0: 0 ====================== DUMP STOP ===================== ------ <span>isset</span> : a ------<span> Cls</span>::__isset is called! ["a"<span>] </span><span>false</span> ------ <span>isset</span> : b ------<span> Cls</span>::__isset is called! ["b"<span>] </span><span>false</span> ------ <span>isset</span> : c ------<span> Cls</span>::__isset is called! ["c"<span>] </span><span>false</span> ------ <span>isset</span> : d ------<span> Cls</span>::__isset is called! ["d"<span>] </span><span>false</span> ------ <span>isset</span> : e ------<span> Cls</span>::__isset is called! ["e"<span>] </span><span>false</span> ------ <span>isset</span> : f ------<span> Cls</span>::__isset is called! ["f"<span>] </span><span>false</span> ====================== DUMP START ====================<span> Cls</span>::__get is called! ["c"<span>] Cls</span>::__get is called! ["d"<span>] a</span>: 0<span> b</span>: 1<span> c</span>:<span> d</span>:<span> Property </span>0: 0 ====================== DUMP STOP =====================
特别强调,当这个属性不存在(基于当前访问权限)的时候,才会触发这些方法。因此严重建议使用确定的键值进行get/set处理,特别是对应的键值不要有访问权限问题,如上例中的a和b,会导致在类内部、类外部、子类内处理的方式不同,开发时产生不可预知的返回值,极易产生bug。
empty方法不能调用属性重载方法。
<span>$tmp</span> = <span>$obj</span>-><span>$char</span> = <span>$char</span> . "-val"
此例中,__set自身的返回值将被忽略,同样__get也不会被调用。
此外:属性重载方法必须声明为public,同时参数不能使用引用传递,静态属性不能通过这些方法重载,这些方法也不能被声明为static。
当然,使用魔术方法的时候,也会对反射系统造成影响。