Blogger Information
Blog 5
fans 0
comment 0
visits 4430
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示子类的三个应用场景
小码哥的博客
Original
762 people have browsed it

<?php
/**
* Created by PhpStorm.
* User: dell
* Date: 2019/10/4
* Time: 21:20
*/

class MyClass{
   public function __construct()
   {
       echo "__construct";
   }

   public function MyPublic(){
       echo "MyPublic";
   }

   protected function MyProtected(){
       echo "MyProtected";
   }

   private function MyPrivate(){
       echo "MyPrivate";
   }

   function Foo(){
       $this->MyPublic();
       $this->MyProtected();
       $this->MyPrivate();
   }
}

$myclass = new MyClass();
$myclass->MyPublic();    //这行能正常执行
$myclass->MyProtected(); //这里会发生致命错误
$myclass->MyPrivate(); //同上

$myclass->Foo();

class MyClass2 extends MyClass{

   function Foo2(){
       $this->MyPublic();
       $this->Myprotected();
       $this->MyPrivate();   //这行会产生一个致命错误

   }
}

$myclass2 = new MyClass2();
$myclass2->MyPublic();  //这行能被正常执行
$myclass2->Foo2();  //公有和保护的都可以执行,私有的不可执行

Correction status:qualified

Teacher's comments:总结的不错, 在项目要灵活应用
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!