<?php /** * Created by PhpStorm. * User: dell * Date: 2019/10/4 * Time: 11:31 */ class MyClass{ public $public = 'Public' ; // 父类 protected $protected = 'Protected' ; // 公有变量 private $private = 'Private' ; // 私有变量 function printHello (){ echo $this -> public ; echo '<br>' ; echo $this -> protected ; echo '<br>' ; echo $this -> private ; } } $obj = new MyClass() ; echo $obj -> public ; echo '<br>' ; echo $obj -> printHello () ; class MyClass2 extends MyClass{ protected $protected = 'Protected2' ; // 重定义方法 function printHello (){ echo $this -> public ; echo '<br>' ; echo $this -> protected ; } } $obj2 = new MyClass2() ; echo $obj2 -> public ; echo $obj2 -> printHello () ;