📜  PHP的public、private 和 protected 之间有什么区别?

📅  最后修改于: 2022-05-13 01:54:11.444000             🧑  作者: Mango

PHP的public、private 和 protected 之间有什么区别?

public、private 和protected 被称为访问修饰符。就像 C++ 一样, PHP也有三个访问修饰符,如 public、private 和 protected。属性、方法或常量的可见性可以通过在声明前加上这些关键字来定义。

  • 如果类成员声明为 public 则它可以在任何地方访问。
  • 如果类成员声明为受保护,则只能在类本身内以及通过继承类和父类访问它。
  • 如果类成员声明为私有,则它只能由定义该成员的类访问。

公共访问修饰符:该修饰符可以在类内部和外部使用。
例子:

php
tag_line."
";     } }   // SubClass class child extends pub {     function show(){         echo $this->tag_line;     } }   // Object Declaration $obj= new child;   // A Computer Science Portal for Geeks! echo $obj->tag_line."
";   // A Computer Science Portal for Geeks! $obj->display();   // A Computer Science Portal for Geeks! $obj->show(); ?>


php
x-$this->y . "
";     }     }   // SubClass - Inherited Class class child extends pro {     function mul() //Multiply Function     {         echo $sub=$this->x*$this->y;     } }   $obj= new child; $obj->sub(); $obj->mul(); ?>


php
name;
    }
}
 
// Object Declaration
$obj= new child;
 
// Uncaught Error: Call to private method demo::show()
$obj->show();
 
//Undefined property: child::$name
$obj->display();
?>


php
public;
        echo $this->protected;
        echo $this->private;
    }
}
 
$obj = new BaseClass();
echo $obj->public;
echo $obj->protected; // Cannot access protected property
echo $obj->private; // Cannot access private property
$obj->Display();  //Displays all properties
 
class SubClass extends BaseClass
{
    public $public = 'Public Sub Class';
    protected $protected = 'Protected Sub Class';
 
    function Display()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}
 
$obj2 = new SubClass();
echo $obj2->public;
echo $obj2->protected; // Cannot access protected property
echo $obj2->private;  // Cannot access private property
$obj2->Display(); //Displays all properties
?>


输出:

A Computer Science Portal for Geeks!
A Computer Science Portal for Geeks!
A Computer Science Portal for Geeks!

受保护的访问修饰符:此修饰符在定义它的类及其父类或继承类中开放使用。
例子:

PHP

x-$this->y . "
";     }     }   // SubClass - Inherited Class class child extends pro {     function mul() //Multiply Function     {         echo $sub=$this->x*$this->y;     } }   $obj= new child; $obj->sub(); $obj->mul(); ?>

输出:



0
250000

私有访问修饰符:此修饰符在定义它的类中开放使用。 (它不能在类之外访问,意味着在继承的类中)。
例子:

PHP

name;
    }
}
 
// Object Declaration
$obj= new child;
 
// Uncaught Error: Call to private method demo::show()
$obj->show();
 
//Undefined property: child::$name
$obj->display();
?>

输出:

It will display error because private class data can not be accessed outside the class

其他示例:
例子:

PHP

public;
        echo $this->protected;
        echo $this->private;
    }
}
 
$obj = new BaseClass();
echo $obj->public;
echo $obj->protected; // Cannot access protected property
echo $obj->private; // Cannot access private property
$obj->Display();  //Displays all properties
 
class SubClass extends BaseClass
{
    public $public = 'Public Sub Class';
    protected $protected = 'Protected Sub Class';
 
    function Display()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}
 
$obj2 = new SubClass();
echo $obj2->public;
echo $obj2->protected; // Cannot access protected property
echo $obj2->private;  // Cannot access private property
$obj2->Display(); //Displays all properties
?>

输出:

It will display error because private class data can not be accessed outside the class

PHP是一种专门为 Web 开发设计的服务器端脚本语言。您可以按照此PHP教程和PHP示例从头开始学习PHP 。