Heim  >  Artikel  >  php教程  >  [转]php的public、protected、private三种访问控制模式的区别

[转]php的public、protected、private三种访问控制模式的区别

WBOY
WBOYOriginal
2016-06-06 19:40:191295Durchsuche

FROM :http://blog.163.com/weiwenjuan_bj/blog/static/14035033620129304183850/?suggestedreading public:公有类型 在子类中可以通过self::var调用public方法或属性,parent::method调用父类方法 在实例中可以能过$obj-var来调用 public类型的方法或属性 pr

FROM : http://blog.163.com/weiwenjuan_bj/blog/static/14035033620129304183850/?suggestedreading

public: 公有类型

        在子类中可以通过self::var调用public方法或属性,parent::method调用父类方法

        在实例中可以能过$obj->var 来调用 public类型的方法或属性

protected: 受保护类型
        在子类中可以通过self::var调用protected方法或属性,parent::method调用父类方法

        在实例中不能通过$obj->var 来调用  protected类型的方法或属性

private: 私有类型
 该类型的属性或方法只能在该类中使用,在该类的实例、子类中、子类的实例中都不能调用私有类型的属性和方法


2.self 和 parent 的区别
  a).在子类中常用到这两个对像。他们的主要区别在于self可以调用父类中的公有或受保护的属性,但parent不可以调用

  b).self:: 它表示当前类的静态成员(方法和属性) 与 $this 不同,$this是指当前对像

附代码:

<span> 1</span> <span>php
</span><span> 2</span> <span>/*</span><span>*
</span><span> 3</span> <span> * parent 只能调用父类中的公有或受保护的方法,不能调用父类中的属性
</span><span> 4</span> <span> * self  可以调用父类中除私有类型的方法和属性外的所有数据
</span><span> 5</span>  <span>*/</span>
<span> 6</span> <span>class</span><span> User{
</span><span> 7</span>     <span>public</span> <span>$name</span><span>;
</span><span> 8</span>     <span>private</span> <span>$passwd</span><span>;
</span><span> 9</span>     <span>protected</span> <span>$email</span><span>;    
</span><span>10</span>     <span>public</span>  <span>function</span><span> __construct(){
</span><span>11</span>         <span>//</span><span>print __CLASS__." ";</span>
<span>12</span>         <span>$this</span>->name= 'simple'<span>;
</span><span>13</span>         <span>$this</span>->passwd='123456'<span>;
</span><span>14</span>         <span>$this</span>->email = 'bjbs_270@163.com'<span>;
</span><span>15</span> <span>    }    
</span><span>16</span>     <span>public</span> <span>function</span><span> show(){
</span><span>17</span>         <span>print</span> "good "<span>;
</span><span>18</span> <span>    }    
</span><span>19</span>     <span>public</span> <span>function</span><span> inUserClassPublic() {
</span><span>20</span>         <span>print</span> <span>__CLASS__</span>.'::'.<span>__FUNCTION__</span>." "<span>;
</span><span>21</span> <span>    }    
</span><span>22</span>     <span>protected</span>  <span>function</span><span> inUserClassProtected(){
</span><span>23</span>         <span>print</span> <span>__CLASS__</span>.'::'.<span>__FUNCTION__</span>." "<span>;
</span><span>24</span> <span>    }    
</span><span>25</span>     <span>private</span> <span>function</span><span> inUserClassPrivate(){
</span><span>26</span>         <span>print</span> <span>__CLASS__</span>.'::'.<span>__FUNCTION__</span>." "<span>;
</span><span>27</span> <span>    }
</span><span>28</span> <span>}
</span><span>29</span> 
<span>30</span> <span>class</span> simpleUser <span>extends</span><span> User {    
</span><span>31</span>     <span>public</span> <span>function</span><span> __construct(){        
</span><span>32</span>         <span>//</span><span>print __CLASS__." ";</span>
<span>33</span>         parent::<span>__construct();
</span><span>34</span> <span>    }
</span><span>35</span>     
<span>36</span>     <span>public</span> <span>function</span><span> show(){
</span><span>37</span>         <span>print</span> <span>$this</span>->name."//public "<span>;        
</span><span>38</span>         <span>print</span> <span>$this</span>->passwd."//private "<span>;
</span><span>39</span>         <span>print</span> <span>$this</span>->email."//protected "<span>;
</span><span>40</span> <span>    }
</span><span>41</span>     
<span>42</span>     <span>public</span> <span>function</span><span> inSimpleUserClassPublic() {
</span><span>43</span>         <span>print</span> <span>__CLASS__</span>.'::'.<span>__FUNCTION__</span>." "<span>;
</span><span>44</span> <span>    }
</span><span>45</span>     
<span>46</span>     <span>protected</span> <span>function</span><span> inSimpleUserClassProtected(){
</span><span>47</span>         <span>print</span> <span>__CLASS__</span>.'::'.<span>__FUNCTION__</span>." "<span>;
</span><span>48</span> <span>    }
</span><span>49</span>     
<span>50</span>     <span>private</span> <span>function</span><span> inSimpleUserClassPrivate() {
</span><span>51</span>         <span>print</span> <span>__CLASS__</span>.'::'.<span>__FUNCTION__</span>." "<span>;
</span><span>52</span> <span>    }
</span><span>53</span> <span>}
</span><span>54</span> 
<span>55</span> <span>class</span> adminUser <span>extends</span><span> simpleUser {
</span><span>56</span>     <span>protected</span> <span>$admin_user</span><span>;
</span><span>57</span>     <span>public</span> <span>function</span><span> __construct(){
</span><span>58</span>         <span>//</span><span>print __CLASS__." ";</span>
<span>59</span>         parent::<span>__construct();
</span><span>60</span> <span>    }
</span><span>61</span>     
<span>62</span>     <span>public</span> <span>function</span><span> inAdminUserClassPublic(){
</span><span>63</span>         <span>print</span> <span>__CLASS__</span>.'::'.<span>__FUNCTION__</span>." "<span>;
</span><span>64</span> <span>    }
</span><span>65</span>     
<span>66</span>     <span>protected</span> <span>function</span><span> inAdminUserClassProtected(){
</span><span>67</span>         <span>print</span> <span>__CLASS__</span>.'::'.<span>__FUNCTION__</span>." "<span>;
</span><span>68</span> <span>    }
</span><span>69</span>     
<span>70</span>     <span>private</span> <span>function</span><span> inAdminUserClassPrivate(){
</span><span>71</span>         <span>print</span> <span>__CLASS__</span>.'::'.<span>__FUNCTION__</span>." "<span>;
</span><span>72</span> <span>    }
</span><span>73</span> <span>}
</span><span>74</span> 
<span>75</span> <span>class</span> administrator <span>extends</span><span> adminUser {
</span><span>76</span>     <span>public</span> <span>function</span><span> __construct(){        
</span><span>77</span>         parent::<span>__construct();
</span><span>78</span> <span>    }
</span><span>79</span> <span>}
</span><span>80</span> 
<span>81</span> <span>/*</span><span>*
</span><span>82</span> <span> * 在类的实例中 只有公有属性和方法才可以通过实例化来调用
</span><span>83</span>  <span>*/</span>
<span>84</span> <span>$s</span> = <span>new</span><span> administrator();
</span><span>85</span> <span>print</span> '-------------------'<span>;
</span><span>86</span> <span>$s</span>-><span>show();
</span><span>87</span> ?>

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:配置php自带的mail功能Nächster Artikel:修改PHP的memory