PHP受保护的类和私有类什么区别

时间:2023-03-08 23:36:31
PHP受保护的类和私有类什么区别
受保护的继承后可以访问,私有的只能在该类中访问,不会被继承访问
class Man{
protected $name='lee';//受保护
private $age=123;//私有
function __construct(){
echo $this->name;//lee
echo $this->age;//123 } }
class Girl extends Man{
function __construct(){
echo $this->name;//lee
echo $this->age;//不会出现123, } } new Man(); new Girl();
<?php
class Man{
protected $name='lee';//受保护
private $age=123;//私有
function __construct(){
echo $this->name;//lee
echo $this->age;// } }
class Girl extends Man{
function __construct(){
echo $this->name;//lee
echo $this->age;//不会出现123, } } new Man(); new Girl();

PHP受保护的类和私有类什么区别