admin 发表于 2020-3-20 21:01:34

PHP中private、public、protected的区别详解

<div id="cnblogs_post_body" class="blogpost-body " style="margin-bottom: 20px; word-break: break-word; font-family: tahoma, arial, sans-serif; font-size: 12px;"><p style="margin: 10px auto; line-height: 1.5 !important;">先简单粗俗的描述下:<br>public 表示全局,类内部外部子类都可以访问;<br>private表示私有的,只有本类内部可以使用;<br>protected表示受保护的,只有本类或子类或父类中可以访问;</p><p style="margin: 10px auto; line-height: 1.5 !important;">再啰嗦的解释下:</p><p style="margin: 10px auto; line-height: 1.5 !important;">一、</p><p style="margin: 10px auto; line-height: 1.5 !important;">public: 公有类型</p><p style="margin: 10px auto; line-height: 1.5 !important;"><span class="Apple-converted-space">在子类中可以通过 self::属性名(或方法名)&nbsp;<span class="Apple-converted-space">&nbsp;调用public方法或属性,parent::方法名 调用父类方法</span></span></p><p style="margin: 10px auto; line-height: 1.5 !important;">在实例中可以能过 $obj-&gt;属性名(或方法名) 来调用 public类型的方法或属性</p><p style="margin: 10px auto; line-height: 1.5 !important;">protected: 受保护类型</p><p style="margin: 10px auto; line-height: 1.5 !important;"><span class="Apple-converted-space">在子类中可以通过 self::属性名(或方法名) 调用protected方法或属性,parent::属性名(或方法名) 调用父类方法。</span></p><p style="margin: 10px auto; line-height: 1.5 !important;"><span class="Apple-converted-space"><strong><span style="color: rgb(255, 0, 0);">在实例中不能通过 $obj-&gt;属性名(或方法名) 来调用&nbsp;<span class="Apple-converted-space">&nbsp;protected类型的方法或属性</span></span></strong></span>&nbsp; &nbsp;</p><p style="margin: 10px auto; line-height: 1.5 !important;">private: 私有类型<br><span class="Apple-converted-space">该类型的属性或方法只能在该类中使用,</span></p><p style="margin: 10px auto; line-height: 1.5 !important;"><span class="Apple-converted-space"><strong><span style="color: rgb(255, 0, 0);">在该类的实例、子类中、子类的实例中都不能调用私有类型的属性和方法</span></strong></span></p><p style="margin: 10px auto; line-height: 1.5 !important;">二、<br>self 和parent 的区别<br>&nbsp;<span class="Apple-converted-space">&nbsp;a).在子类中常用到这两个对像。他们的主要区别在于self可以调用父类中的公有或受保护的属性,但parent不可以调用</span></p><p style="margin: 10px auto; line-height: 1.5 !important;">&nbsp;<span class="Apple-converted-space">&nbsp;b).self:: 它表示当前类的静态成员(方法和属性)与 $this不同,$this是指当前对像</span></p><p style="margin: 10px auto; line-height: 1.5 !important;"><span class="Apple-converted-space">直接上一示例:</span></p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; line-height: 1.5 !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px; line-height: 1.5 !important;"><br></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">&lt;?<span style="line-height: 1.5 !important;">php
</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">父类</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">class</span><span style="line-height: 1.5 !important;"> father{
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> a(){
      </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> "public function a".<span style="color: rgb(255, 0, 255); line-height: 1.5 !important;">PHP_EOL</span><span style="line-height: 1.5 !important;">;
    }
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">private</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> b(){
      </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> "private function b".<span style="color: rgb(255, 0, 255); line-height: 1.5 !important;">PHP_EOL</span><span style="line-height: 1.5 !important;">;
    }
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">protected</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> c(){
      </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> "protected function c".<span style="color: rgb(255, 0, 255); line-height: 1.5 !important;">PHP_EOL</span><span style="line-height: 1.5 !important;">;
    }
}
</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">子类</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">class</span> child <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">extends</span><span style="line-height: 1.5 !important;"> father{
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> d(){
      parent</span>::a();<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">调用父类的a方法</span>
<span style="line-height: 1.5 !important;">    }
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> e(){
      parent</span>::c(); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">调用父类的c方法</span>
<span style="line-height: 1.5 !important;">    }
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> f(){
      parent</span>::b(); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">调用父类的b方法</span>
<span style="line-height: 1.5 !important;">    }
}
</span><span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$father</span>=<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span><span style="line-height: 1.5 !important;"> father();
</span><span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$father</span>-&gt;a(); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">输出 public function a</span>
<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$father</span>-&gt;b(); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">显示错误 外部无法调用私有的方法</span>
<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$father</span>-&gt;c(); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">显示错误 外部无法调用受保护的方法</span>
<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$chlid</span>=<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span><span style="line-height: 1.5 !important;"> child();
</span><span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$chlid</span>-&gt;d();<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">输出 public function a</span>
<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$chlid</span>-&gt;e(); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//输出 protected function c</span>
<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$chlid</span>-&gt;f();<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">显示错误 无法调用父类private的方法Uncaught Error: Call to private method father::b() from context 'child'</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px; line-height: 1.5 !important;"><br></div></div><p style="margin: 10px auto; line-height: 1.5 !important;">&nbsp;通过上面实力我们可以看出:</p><p style="margin: 10px auto; line-height: 1.5 !important;">  protected 保护模式 只在类本身及派生类中可访问,那么 这个$chlid-&gt;e(); 调用的是 father 的function c(), 也就说 在child 中调用father的c 是可以的. 但如果你直接用child-&gt;c(), 那就是不行的. 要通过child的e() 中转一下 才能调用到father 的c()</p><p style="margin: 10px auto; line-height: 1.5 !important;">再来举例说明:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; line-height: 1.5 !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px; line-height: 1.5 !important;"><br></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;">&lt;?<span style="line-height: 1.5 !important;">php
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">class</span><span style="line-height: 1.5 !important;"> Man {
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">private</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$name</span> = '周伯通'<span style="line-height: 1.5 !important;">;
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">protected</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$money</span> = 3000<span style="line-height: 1.5 !important;">;
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$age</span> = 30<span style="line-height: 1.5 !important;">;

    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> say() {
      </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> '我叫',<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$this</span>-&gt;name,'&lt;br /&gt;'<span style="line-height: 1.5 !important;">;
      </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> '我有',<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$this</span>-&gt;money,'元钱&lt;br /&gt;'<span style="line-height: 1.5 !important;">;
      </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> '我今年',<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$this</span>-&gt;age,'岁'<span style="line-height: 1.5 !important;">;
    }
}

</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">class</span> Stu <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">extends</span><span style="line-height: 1.5 !important;"> Man {
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">private</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$friend</span> = '小白'<span style="line-height: 1.5 !important;">;
    </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">public</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">function</span><span style="line-height: 1.5 !important;"> talk() {
      </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> '我叫',<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$this</span>-&gt;name,'&lt;br /&gt;'<span style="line-height: 1.5 !important;">;//我叫 Notice: Undefined property: Stu::$name in D:\web\mytest\p.php on line 18
      </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> '我有',<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$this</span>-&gt;money,'元钱&lt;br /&gt;'<span style="line-height: 1.5 !important;">; //我有3000元钱
      </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> '我今年',<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$this</span>-&gt;age,'岁&lt;br /&gt;'<span style="line-height: 1.5 !important;">;//我今年30岁
    }
}
</span><span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$class</span> = <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span><span style="line-height: 1.5 !important;"> Stu();
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$class</span>-&gt;age,'&lt;br /&gt;'; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 28</span>

<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$class</span>-&gt;friend; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">出错:因为类外不能调用private</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">echo</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$class</span>-&gt;money; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">出错:因为类外不能调用protected属性</span>
<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">$class</span>-&gt;talk();</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px; line-height: 1.5 !important;"><br></div></div><p style="margin: 10px auto; line-height: 1.5 !important;">以上输出:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: &quot;Courier New&quot; !important; line-height: 1.5 !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px; line-height: 1.5 !important;"><br></div><pre style="white-space: pre-wrap; font-family: &quot;Courier New&quot; !important;"><span style="line-height: 1.5 !important;">30
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&lt;</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">br </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">/&gt;</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&lt;</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">br </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">/&gt;</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&lt;</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">b</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&gt;</span>Fatal error<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&lt;/</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">b</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&gt;</span><span style="line-height: 1.5 !important;">:Uncaught Error: Cannot access private property Stu::$friend in D:\web\mytest\p.php:26
Stack trace:
#0 {main}
thrown in
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&lt;</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">b</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&gt;</span>D:\web\mytest\p.php<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&lt;/</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">b</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&gt;</span><span style="line-height: 1.5 !important;"> on line
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&lt;</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">b</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&gt;</span>26<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&lt;/</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">b</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&gt;</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">&lt;</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">br </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">/&gt;</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px; line-height: 1.5 !important;"><br></div></div><p style="margin: 10px auto; line-height: 1.5 !important;">分析原因: Undefined property: Stu::$name 这是说明:stu对象 没有name属性 但上面不是说私有的不是可以继承吗? 是可以继承过来,但系统有标记,标记其为父类层面的私有属性. 因此无权调用,导致此错发生. 可以分析出: protected 可以在 子类内访问</p><p style="margin: 10px auto; line-height: 1.5 !important;">protected能在子类访问,本类内能否访问? 答:当然可以</p></div><div id="MySignature" style="font-family: tahoma, arial, sans-serif; font-size: 12px;">无论从事什么行业,只要做好两件事就够了,一个是你的专业、一个是你的人品,专业决定了你的存在,人品决定了你的人脉,剩下的就是坚持,用善良專業和真诚赢取更多的信任。</div><p></p>
页: [1]
查看完整版本: PHP中private、public、protected的区别详解